Android Tutorial Material Design 3 - Using a ToolBar as ActionBar

Note: You should use B4A 6.0 or above for this tutorial.

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

First we need to set up our project like in the Material Design 2 tutorial.
Then we need to add some more items to the theme:

B4X:
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>

This disables the standard ActionBar in the theme. With the windowActionModeOverlay set to true ActionMode Actionbars will replace the ToolBar and are not displayed above it.

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:

Layout_ActionBar.png


Note that "Use as main ActionBar" is checked to use this Toolbar as the main ActionBar for the Activity. You can only use one Toolbar as the Activity ActionBar.
Elevation should be set to 4 for a normal ActionBar. This will produce a small shadow below it.
Additionally you should disable the Background color in the properties.

There are two versions of the ToolBar object. ACToolBarLight uses a light theme and ACToolBarDark uses a dark theme by default. This is only for historical reasons. You can set the light or dark theme for the ToolBar and the overflow menu with the designer properties now.

Now we add a Panel named pContent to the Layout with the following properties:

Layout_pContent.png


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:
B4X:
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 have a minimal example of how to setup a ToolBar as an ActionBar.

ToolBar_Shadow.png


Misc stuff

You can use the ACActionBar object to control some ActionBar features like showing the "Up" indicator arrow.

B4X:
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:

B4X:
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.

ExampleApp.png
 

Attachments

  • ACToolBarExample2_0.zip
    25.3 KB · Views: 3,761
Last edited:

wes58

Active Member
Licensed User
Longtime User
I am not sure whether to post it in this thread or in GoogleMapsExtras.
I have an application that was working OK until I decided to change it to use this ToolBar as an ActionBar.
I have an error (which some people already mentioned before):
B4X:
java.lang.ClassCastException: com.x10.Home.main cannot be cast to android.support.v7.app.ActionBarActivity
    at de.amberhome.objects.appcompat.ACToolBarWrapper.SetAsActionBar(ACToolBarWrapper.java:244)
    at com.x10.Home.main._activity_create(main.java:660)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
I have the following defined in my Main activity:
B4X:
    #AdditionalRes: ..\resources
    #AdditionalRes: D:\android-sdk\b4aNewLib\res\b4a_appcompat, de.amberhome.objects.appcompat
    #AdditionalRes: D:\android-sdk\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
    #AdditionalRes: D:\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    #Extends: android.support.v7.app.ActionBarActivity
    #Extends: uk.co.martinpearman.java.android.app.StreetViewPanoramaViewActivity
I have found out that when I remove "#Extends: uk.co.martinpearman.java.android.app.StreetViewPanoramaViewActivity" the application works OK.
Application was working fine with the StreetViewPanorama included when I used the ahaActionBar.

Any suggestions what can be wrong?

Edit:
I have found out that it is a problem with the GoogleMapsExtras. StreetViewPanoramaViewActivity in the library has to extend ActionBarActivity, currently it extends Activity.
I will post it in GoogleMapsExtras thread.
 
Last edited:

CanguroCode

Active Member
Licensed User
Longtime User
The previous examples worked well for me. But this example gives me the following error:

B4X:
LogCat connected to: 192.168.31.101:5555
--------- beginning of /dev/log/main


--------- beginning of /dev/log/system
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.amberhome.appcompat.toolbarexample1/de.amberhome.appcompat.toolbarexample1.main}: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
    at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:360)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:237)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:111)
    at de.amberhome.appcompat.toolbarexample1.main.onCreate(main.java:59)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    ... 11 more

What is the error? It's wrong?

The project attached to this post. Thanks

Edit1:

I think the mistake has to do with this line in the custom theme:

B4X:
        <item name="windowActionBar">false</item>
But I do not know what is lacking
 

Attachments

  • Test1.zip
    21.5 KB · Views: 372
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
I think that you are right as the error message is stating a problem with the theme...
Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
 

corwin42

Expert
Licensed User
Longtime User
I think the mistake has to do with this line in the custom theme:

B4X:
        <item name="windowActionBar">false</item>
But I do not know what is lacking

Very close to the solution. AppCompat V22.1 is more strict with the theme and makes more checks. I have added a section about this error in the first post of the Thread to the AppCompat library. If I remember correctly in the theme we have an item "android:windowNoTitle" (which is set to true). This has to be only "windowNoTitle".

That should fix the problem.
 

CanguroCode

Active Member
Licensed User
Longtime User
Very close to the solution. AppCompat V22.1 is more strict with the theme and makes more checks. I have added a section about this error in the first post of the Thread to the AppCompat library. If I remember correctly in the theme we have an item "android:windowNoTitle" (which is set to true). This has to be only "windowNoTitle".

That should fix the problem.

Your are right. My code of the XML Theme:

B4X:
<?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="windowNoTitle">True</item>
         <item name="windowActionBar">false</item>
     
    </style>
</resources>

I also tried the following and it worked:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyAppTheme" parent="Theme.AppCompat.NoActionBar">
 
        <item name="colorPrimary">#FF9800</item>
        <item name="colorPrimaryDark">#F57C00</item>
        <item name="colorAccent">#FFA726</item>
        <item name="windowNoTitle">True</item>
     
    </style>
</resources>

Solved! Thanks Corwin
 
Last edited:

wes58

Active Member
Licensed User
Longtime User
I was silly enough to start today google SDK Manager to check for updates. After that, my two applications after compilation don't work - I get a black screen on start up of those application without any errors. I tried compiling them on two separate phones and the same issue
I even tried to compile an example from post #1 and it is the same story - black screen.

In both of them I am using AppCompat library, using ToolBar as ActionBar. So, I believe it must have something to do with the new version of android-support-v7-appcompat.jar.
Has anybody had any similar problems?
 

corwin42

Expert
Licensed User
Longtime User
I was silly enough to start today google SDK Manager to check for updates. After that, my two applications after compilation don't work - I get a black screen on start up of those application without any errors. I tried compiling them on two separate phones and the same issue
I even tried to compile an example from post #1 and it is the same story - black screen.

In both of them I am using AppCompat library, using ToolBar as ActionBar. So, I believe it must have something to do with the new version of android-support-v7-appcompat.jar.
Has anybody had any similar problems?
See one message above yours.

Be sure that you copy the latest android-support-v4.jar and android-support-v7.jar to your customlibs folder.
 

wes58

Active Member
Licensed User
Longtime User
See one message above yours.

Be sure that you copy the latest android-support-v4.jar and android-support-v7.jar to your customlibs folder.
I use in my application #AdditionalRes statement which points to the location in android-sdk to the latest version of the jars (the same way as you did in your example project) so I don't know what else can be wrong.

Edit:
I read your comments about the changes in the new version and I found a problem. It is fixed and working OK now.
Thanks for your help.
 
Last edited:

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

Can we use a materialized navigation drawer with this toolbar (or even with the ActionBar that is mentioned in the 2nd tutorial on Material) implementation? If so, how to get started on that?

Many thanks.
 

Haris Hafeez

Active Member
Licensed User
Longtime User

skeat

New Member
Licensed User
Longtime User
Hi,

I am using ACToolBar as activity's ActionBar. Is there a way to detect when its overflow menu open?
Similar to http://stackoverflow.com/questions/23806383/actionbars-overflow-menu-open-close-listener

Any such event available in b4a for ActionBar?

Or, is there a way to read the state of overflow menu in ActionBar just like ACActionMenu.OverflowMenuOpen(Shows/hides the overflow menu or return if overflow menu is currently showing) ?
 

jotaele

Member
Licensed User
Longtime User
Can you show an example what the problem looks like?



Yes, just add a TabBar directly under the Toolbar.

Two questions about:

TabBar is a library? I cant find info. Can you help me.

Is posible to change the background color of the menu?

I tried to put an icon in the overflow menu with Activity.AddMenuItem2, but not show the icon. Is it normal?

B4X:
Dim s2 As Bitmap
s2.Initialize(File.DirAssets, "icono.png")
Activity.AddMenuItem2("Cambiar Nombre", "AccionCambiarNombre", s2)

Thanks in average.
 
Last edited:

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Trying to compile the example I get a lot of:

C:\b4android\AndrioidSdk\extras\android\support\v7\appcompat\res\values-v21\themes_base.xml:90: error:
Error: No resource found that matches the given name: attr 'android:colorControlActivated'.

Any idea?
 

corwin42

Expert
Licensed User
Longtime User
New game here in the forum: Guess the error :)

My try: You are not referencing an android.jar with at least version 21 in your "Configure Paths" dialog of the IDE. Always use the latest (official) android.jar to compile your projects with AppCompat.
 
Last edited:

jahswant

Well-Known Member
Licensed User
Longtime User
Ok i was reluctant to update the sdk(appcompact).But this i decided to go through but BANG.Any compile with the appcompact lib gives me a black screen.Is there a possible reason.
NB:I just tried modifying the theme file as stated but the pb persists.
 

corwin42

Expert
Licensed User
Longtime User
Ok, I know that the AppCompat library is quite complicated to use. You have to setup everything right with sdk versions, themes, #AdditionalRes attributes etc.
I myself stepped in many traps while developing and using this library.

But please try to give relevant information if you have a problem using this library. Just a "I updated this morning" (updated what? Support Library, Your Android version on your device, B4A IDE, ...) is not something that really helps finding out what the problem is.
 
Top