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,759
Last edited:

corwin42

Expert
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.

Do you get any errors? Never had a "black screen" without any other error. Do you see something in the unfiltered logs (connected via USB)?
 

jahswant

Well-Known Member
Licensed User
Longtime User
The activity doesn't even start at all.Just a black screen with no compilation error.
 

corwin42

Expert
Licensed User
Longtime User
The activity doesn't even start at all.Just a black screen with no compilation error.
Just read the section about support library 22.1 and above in OP of this thread. I'm 100% sure that is the problem.

btw: A "black screen" is not the same as "Activity is not starting at all". Thats the problem with such issue reports. Most people don't write exactly what they see or have done. And it is very time consuming for us library developers to guess what the problem may be.
 

thedesolatesoul

Expert
Licensed User
Longtime User
Just read the section about support library 22.1 and above in OP of this thread. I'm 100% sure that is the problem.

btw: A "black screen" is not the same as "Activity is not starting at all". Thats the problem with such issue reports. Most people don't write exactly what they see or have done. And it is very time consuming for us library developers to guess what the problem may be.
Also a lot of users report a black screen even when there is a perfectly debuggable crash and the stack trace is in the logs.
As google iterates through their libraries i've seen there mainly to be three reasons:
1. The android.jar version B4A refers to
2. The version of the support libs
3. The version of the resource files (may be compatible with an older version but not new one)
 

Anser

Well-Known Member
Licensed User
Longtime User
I understand that the Click on the UpArrowIndicator does not work without an overflow menu on the ActionBar. In other words, the Click on the UpArrowIndicator works only if there is an overflow menu on the ToolBar. How to overcome this issue ? Is there any specific reason for that ? or am I missing something ?

After spending so much time, I found that the work around to solve this problem is to add an OverFlow Menu, but it doesn't seem to be an elegant solution.

B4X:
ABHelper.Initialize
ABHelper.ShowUpIndicator = True
 
' This will create the OverFlow Menu. If this is not added then the Back button on the left side of the Toolbar does not work
Activity.AddMenuItem("Go Back", "Menu_GoBack")

'I Capture the UpIndicator Click using the below given Sub. Unfortunately this does not get executed if I don't add an overflow Menu to the ActionBar
Sub Activity_ActionBarHomeClick
    Activity.Finish
End Sub

'Capture the click on the OverFlow Menu Item and then re-direct to Activity_ActionBarHomeClick
Sub Menu_GoBack_Click
    Activity_ActionBarHomeClick
End Sub

I am herewith pasting the whole code, so that someone can verify whether I am doing it in a wrong way
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
#Extends: android.support.v7.app.ActionBarActivity

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private ActionBar As ACToolBarLight
    Private lvCallsList As ListView
 
    Dim AC As AppCompat
    Dim ABHelper As ACActionBar 
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("CallsList")
 
    '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 = "My Title"
    ActionBar.SubTitle = "My SubTitle"
 
    ' Set the Toolbar Shadow
    AC.SetElevation(ActionBar, 8dip)
 
    ABHelper.Initialize
    ABHelper.ShowUpIndicator = True
 
   ' This will create the OverFlow Menu. If this is not added then the Back button 
   ' on the left side of the Toolbar is not working
    Activity.AddMenuItem("Go Back", "Menu_GoBack")

End Sub

Sub Activity_Resume
 
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_ActionBarHomeClick
    Activity.Finish
End Sub

Sub Menu_GoBack_Click
    Activity_ActionBarHomeClick
End Sub

I don't know whether I could ask doubt in this thread, as I feel that this query is related to this Material Design - Using a ToolBar as ActionBar, I am posting it here.

Any help would be appreciated

Thanks & Regards

Anser
 

corwin42

Expert
Licensed User
Longtime User
The answer is in the last post on the second page of this thread. Use ToolbarXXX.InitMenuListener and the NavigationitemClicked event of the Toolbar.
 

ivan.tellez

Active Member
Licensed User
Longtime User
Hi, this is a really great tutorial.

What is the better option to implement a side menu/drawer whit this toolbar?

Also, when you use:

B4X:
ABHelper.ShowUpIndicator = True

Is there a way to animate the icon?

Thanks
 

ArminKH

Well-Known Member
hi @corwin42
is there any solution to achive to following code ?
B4X:
For i = 0 To AppCombatToolbar.NumberOfViews -1
        AppCombatToolbar.GetView(i).Left = 100%x - AppCombatToolbar.GetView(i).Left - AppCombatToolbar.GetView(i).Width
    Next
this code not worked because the layout of view is not available
i want to reverse the dimension of all view's which is added to toolbar for adjusting the toolbars for write to left languages
tnx
 

fishwolf

Well-Known Member
Licensed User
Longtime User
with the example in page 1 the app doen't start and don't see errors from b4x

from monitor i have this error,

how to i can fix ?

thanks

B4X:
08-13 16:25:04.726: I/B4A(3002): ~e: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
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.os.Looper.loop(Looper.java:137)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.main(ActivityThread.java:4745)
08-13 16:25:04.726: I/B4A(3002): ~e:   at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:25:04.726: I/B4A(3002): ~e:   at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:25:04.726: I/B4A(3002): ~e:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-13 16:25:04.726: I/B4A(3002): ~e:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-13 16:25:04.726: I/B4A(3002): ~e:   at dalvik.system.NativeStart.main(Native Method)
08-13 16:25:04.726: I/B4A(3002): ~e:Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:363)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:237)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:111)
08-13 16:25:04.726: I/B4A(3002): ~e:   at de.amberhome.appcompat.toolbarexample1.main.onCreate(main.java:59)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.Activity.performCreate(Activity.java:5008)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-13 16:25:04.726: I/B4A(3002): ~e:   ... 11 more
 

ArminKH

Well-Known Member
with the example in page 1 the app doen't start and don't see errors from b4x

from monitor i have this error,

how to i can fix ?

thanks

B4X:
08-13 16:25:04.726: I/B4A(3002): ~e: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
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.os.Looper.loop(Looper.java:137)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.main(ActivityThread.java:4745)
08-13 16:25:04.726: I/B4A(3002): ~e:   at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:25:04.726: I/B4A(3002): ~e:   at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:25:04.726: I/B4A(3002): ~e:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-13 16:25:04.726: I/B4A(3002): ~e:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-13 16:25:04.726: I/B4A(3002): ~e:   at dalvik.system.NativeStart.main(Native Method)
08-13 16:25:04.726: I/B4A(3002): ~e:Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:363)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:237)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:111)
08-13 16:25:04.726: I/B4A(3002): ~e:   at de.amberhome.appcompat.toolbarexample1.main.onCreate(main.java:59)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.Activity.performCreate(Activity.java:5008)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-13 16:25:04.726: I/B4A(3002): ~e:   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-13 16:25:04.726: I/B4A(3002): ~e:   ... 11 more
Change theme from xml file to another one
The error message say this theme is not supported by app combat
 

fishwolf

Well-Known Member
Licensed User
Longtime User
Change theme from xml file to another one
The error message say this theme is not supported by app combat
now is set

B4X:
- <style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

what is a right theme for me?
 

ArminKH

Well-Known Member
now is set

B4X:
- <style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

what is a right theme for me?
You are using toolbar instead default actionbar so your app has not action bar(default action bar)
Change DarkActionBar to NoActionBar
 

corwin42

Expert
Licensed User
Longtime User
I have updated the Example in the attachment of the first post.

It fixes the "AppCompat does not support the current theme features" error. The problem is in the theme.xml file. All attributes MUST NOT have the android: prefix in their name. It is explained in the first post of the AppCompat library.
 

ArminKH

Well-Known Member
I have updated the Example in the attachment of the first post.

It fixes the "AppCompat does not support the current theme features" error. The problem is in the theme.xml file. All attributes MUST NOT have the android: prefix in their name. It is explained in the first post of the AppCompat library.
And what about my problem corwin?
My app language is write to left but the app combat does'n support write to left language
If the layout properties be accessable then i can reverse my view position
Or any suggestion?
 

corwin42

Expert
Licensed User
Longtime User
And what about my problem corwin?
My app language is write to left but the app combat does'n support write to left language
If the layout properties be accessable then i can reverse my view position
Or any suggestion?

Sorry, can you explain what you exactly want to do?
 

ArminKH

Well-Known Member
Sorry, can you explain what you exactly want to do?
yes
i am using AC Toolbar Which is a part of app compat library
we can put a navigation icon , logo icon , search view , menu , ... into this toolbar
this is good for english and all left to write language but if you read google developer documents about right to left language like persian or arabic
this languages are reverse of english
for example if navigation icon is in the left side of toolbar in english language then we should put it to right side
in other word all of views which is in left side must be in right side
please see attached scrren shot
upload_2015-8-18_15-13-40.png

in right to left languages the menu , search view , other extra actions must be in left side
and navigation icon , logo , title and sub title must be in right side of toolbar
https://www.google.com/design/spec/...ectionality-other-localization-considerations
 

Attachments

  • upload_2015-8-18_15-12-58.png
    upload_2015-8-18_15-12-58.png
    105 bytes · Views: 292
Last edited:

corwin42

Expert
Licensed User
Longtime User
yes
i am using AC Toolbar Which is a part of app compat library
we can put a navigation icon , logo icon , search view , menu , ... into this toolbar
this is good for english and all write to left language but if you read google developer documents about right to left language like persian or arabic
this languages are reverse of english
for example if navigation icon is in the left side of toolbar in english language then we should put it to right side
in other word all of views which is in left side must be in right side
please see attached scrren shot
View attachment 36707
in right to

To support RTL languages try to set
B4X:
SetApplicationAttribute(android:supportsRtl, "true")
in the Manifest editor. This will reverse the item order in the ActionBar/Toolbar on RTL devices.
 

ArminKH

Well-Known Member
To support RTL languages try to set
B4X:
SetApplicationAttribute(android:supportsRtl, "true")
in the Manifest editor. This will reverse the item order in the ActionBar/Toolbar on RTL devices.
is this working on all android version?
and if our device language be english but our app's language be for example persian then this attribute works?
----------------------------
edit : @corwin42 when my device language is english then action bar is left to right but when my device language is persian then my actionbar is right to left
is there any way with no dependency on the phone language?
i want the phone's language be english but my actionbar be right to left
 
Last edited:
Top