Google recommends to use a ToolBar instead of the standard system ActionBar for Material Design apps. In this tutorial we will use a ACToolBar(Light/Dark) object from the AppCompat library (1.10 and above) as an ActionBar.
One of the main differences between a ToolBar and the standard ActionBar is that the ActionBar is a system component which is automatically added by the os and a ToolBar is part of our layout so we can add it to the activity with the designer or by code.
Using the ToolBar object
The best approach to learn how to use the ToolBar is by an example. So we create a new project and set it up very similar to the project from the
Material Design 2 tutorial. Here we go with a quick setup:
Add the following code to your activity to add additional ressources and extend the ActionBarActivity:
#AdditionalRes : ..\resource
#AdditionalRes : <Path to appcompat custom libs res>\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes : <Path to Android sdk>\android-sdk\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#Extends : android.support.v7.app.ActionBarActivity
Change the following line in the manifest editor to use our custom theme:
SetApplicationAttribute(android:theme, "@style/MyAppTheme")
We need to create our own theme file. So add the theme.xml file to the resource\values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#FF9800</item>
<item name="colorPrimaryDark">#F57C00</item>
<item name="colorAccent">#FFA726</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
Because we want to use a ToolBar instead of the normal system ActionBar we have to disable the ActionBar in our theme. This is done with
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
Just setting the #IncludeTitle attribute to false does not work here and will produce an error. So we need the two lines in our theme.
Now we can create our layout. For the example project I decided to split the layout into two parts. One "main" layout which contains the ToolBar and a simple Panel for the content.
So add a CustomView of type "ACToolBarLight" to a new layout called "main" and set the following properties:
View attachment 30934
There are two versions of the ToolBar. ACToolBarLight uses a light theme and ACToolBarDark uses a dark theme. Two objects are needed because for designer CustomViews you can't set any initialization parameters and the theme of the ToolBar can't be changed at runtime. It is possible to create a ToolBar by code, too. Be aware that the new layout animation feature of B4A 4.0 and above does not affect any ToolBar.
The theme for the overflow menu can be set at runtime with the ACToolBarLight.PopupTheme property.
Then add a Panel named pContent with the following properties:
View attachment 30935
Because the standard height of the ToolBar/ActionBar in Material Design depends on the device orientation and screen size we add a small designer script:
If ActivitySize > 6.5 Then
ActionBar.Height = 64dip
Else
If Portrait Then
ActionBar.Height = 56dip
Else
ActionBar.Height = 48dip
End If
End If
pContent.SetTopAndBottom(ActionBar.Bottom, 100%y)
This will set the ActionBar height to 64dip on tablets and to 56dip on portrait phones and 48dip on landscape phones. These specifications are in the Material Design guide.
Now we can load our layout to the activity and can set up the ToolBar. There are two possibilities to use ToolBars. As standalone objects or as the system ActionBar. You can only set up one ToolBar as an ActionBar but you can place the ToolBar anywhere on the activity.
Activity.LoadLayout("main")
'Set the ToolBar (it is called ActionBar in the layout file) as the ActionBar of this activity.
ActionBar.SetAsActionBar
'Set Title and Subtitle for the ToolBar
ActionBar.Title = "AppCompat"
ActionBar.SubTitle = "ToolBar Example 1"
So now we have a minimal example of how to setup a ToolBar as an ActionBar.
Adding a shadow
The ToolBar looks nice but the standard ActionBar had a small shadow so it looks like it is floating above the activity. The ToolBar by default don't have this shadow. In the Material Design guidelines it is not really clear if a ToolBar should have a shadow or not. Some of the Google apps have the shadow, others not. The AppCompat wrapper library contains a method to set the elevation of every view so we set a small elevation of 8dip to our ToolBar to get a nice shadow:
Dim AC As AppCompat
AC.SetElevation(ActionBar, 8dip)
The AppCompat.SetElevation() method can set the elevation of every view. Try it with normal Panels for example. Unfortunately the shadow will only show on Android 5.0 or above.
View attachment 30936
Misc stuff
You can use the ACActionBar object to control some ActionBar features like showing the "Up" indicator arrow.
Dim ABHelper as ACActionBar
ABHelper.ShowUpIndicator = True
Adding actions and overflow menu
You can use the normal Activity.AddMenuItem() methods to add a menu or actions to the ToolBar.
If you want to add actions you need some icons. Google released a huge set of Material Design icons wich can be used as action icons. Unfortunately these icons are packed in a bad directory structure which makes it hard to pick a single icon and use it as drawables for the actions. So
I wrote a small tool that reorganizes the icons in a better directory structure.
Ok, if we picked some icons and copied their drawable folders to our resources folder we can start to add some actions and overflow menu items:
Dim xml As XmlLayoutBuilder
Dim bd As BitmapDrawable
bd = xml.GetDrawable("ic_plus_one_black_24dp")
Activity.AddMenuItem3("Plus one", "Menu", bd.Bitmap, True)
bd = xml.GetDrawable("ic_refresh_black_24dp")
Activity.AddMenuItem3("Refresh", "Menu", bd.Bitmap, True)
Activity.AddMenuItem("Overflow1", "Menu")
Activity.AddMenuItem("Overflow2", "Menu")
Activity.AddMenuItem("Overflow3", "Menu")
I prefer to use drawables for action icons than use the LoadBitMap() function. The drawables are available in different resolutions and will always load in the perfect size for your device. To load drawables you will have to use the XmlLayoutBuilder library.
The attached example has some UI elements to control some features of the ToolBar. Have fun with it.
View attachment 30937