Android Tutorial Material Design 4 - Modifyable and advanced Menu

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

With the ACToolBar(Light|Dark) object we can get access to the Menu object of the activity. This opens the possibility for modifyable menus and some other advanced menu features.

Basics

With the ToolBar.Menu property we have access to the menu of the ToolBar. For a standalone ToolBar (which is not set as the activity ActionBar) we can just access the property everywhere in our code and do what we want to do with it just like adding/removing menu items.

If the ToolBar is our activities ActionBar we have to handle the menu in a special way because if we don't do this, B4As own menu handling will conflict with our handling.

In Android the menu is created and initalized with two methods: onCreateOptionsMenu() and onPrepareOptionsMenu(). I don't want to explain the whole menu system here you just need to know, that B4A uses these methods internally to create it's own menu and they get called "at some time" in the activity initialization process.

With the itroduction of inline Java code in B4A 4.30 we now have the possibility to add our own code to handle the application menu. The idea is to call an event to set up the menu and here is an example how this can be done:

B4X:
#If Java

public boolean _onCreateOptionsMenu(android.view.Menu menu) {
    if (processBA.subExists("activity_createmenu")) {
        processBA.raiseEvent2(null, true, "activity_createmenu", false, new de.amberhome.objects.appcompat.ACMenuWrapper(menu));
        return true;
    }
    else
        return false;
}
#End If

In this code we use the _onCreateOptionsMenu hook to check if a Sub named "Activity_CreateMenu" exists. If yes, then we call it and we provide the activities menu as a parameter. If the sub does not exist, the normal B4A menu-handling is done.

Now we need the implementation of Activity_CreateMenu:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
    Menu.Clear

    Dim item As ACMenuItem
    Menu.Add2(10, 1, "Plus one", xml.GetDrawable("ic_plus_one_black_24dp")).ShowAsAction = item.SHOW_AS_ACTION_IF_ROOM
    Menu.Add2(20, 2, "Refresh", xml.GetDrawable("ic_refresh_black_24dp")).ShowAsAction = item.SHOW_AS_ACTION_ALWAYS

    Menu.Add(1, 3, "Overflow1", Null)
    Menu.Add(2, 4, "Overflow2", Null)
    Menu.Add(3, 5, "Overflow3", Null)

    'Sync Checkboxes with the Menu
    SetCheckBoxState(Menu)

End Sub
First we clear the menu to be sure that there are no old entries in it. Then we just use the Menu object to add some additional menus. Easy, isn't it? (For this example we sync the menu with the checkboxes with SetCheckBoxState())

It is good practice to use a Toolbar as your ActionBar. If you do this you can access the menu with ToolBar.Menu everywhere in your code to modify the menu.

Some advanced menu features

With the ACMenu and ACMenuItem objects we have access to some advanced menu features.

When you add a menu item to the menu you can specify a menu ID and a sort order. So with the sort order you can later add menu entries which are displayed above other menu items. With the ID you can access this item later and modify it.

You can create checkable menu itemes. The ACMenu.Add() methods return the added menu item so you can directly modify it and make it checkable or set it as an action item or even make it invisible.

It is even possible to add or remove menu items dynamically.

B4X:
Sub cbItemVisible_CheckedChange(Checked As Boolean)
  ToolBar.Menu.FindItem(1).Visible = Checked
End Sub

Sub cbItemCheckable_CheckedChange(Checked As Boolean)
  ToolBar.Menu.FindItem(2).Checkable = Checked
End Sub

Sub cbItemDisable_CheckedChange(Checked As Boolean)
  ToolBar.Menu.FindItem(3).Enabled = Not(Checked)
End Sub

Look at the example project for better understanding.

overflow_menu.png
 

Attachments

  • ToolBarMenuExample2_0.zip
    18.6 KB · Views: 4,001
Last edited:

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I want to enable the Contextual ActionBar for a listview long click. Anyone know how to do that ?

Regards,
SK

Edit : I got it :) but i am facing one more issue....

I am getting an error that prepare signature is wrong

B4X:
java.lang.Exception: Sub mode_prepared signature does not match expected signature.

What is the proper signature of ACActionMode prepared,created,ItemClicked and closed .

Edit1 : I corrected it. Thank you :)
 
Last edited:

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

I want to change the color of the title of ACActionMode. How do you go about doing that ??

Regards,
SK
 

corwin42

Expert
Licensed User
Longtime User

corwin42

Expert
Licensed User
Longtime User
Can you connect your device via USB and try it again? Or try an Emulator like Genymotion. I think the B4A Bridge does not show the unfiltered logs completely.
 

corwin42

Expert
Licensed User
Longtime User
Without an error message I can't do much. Another question? Which device and Android Version do you use? There are some Samsung (and other) devices with a conflicting built in support library.
 

corwin42

Expert
Licensed User
Longtime User
This might be the problem since I am testing this on a Samsung Galaxy Note 4 running Lollipop 5.0.1 (Touchwiz) and a Samsung Galaxy S4 mini running 4.4.2 (Touchwiz). I will probably install Cyanogenmod one of the next few days and I'll keep you posted. Thank you for for your help.
No, the faulty Samsung Android version was a 4.2.2 one. Your problem is another one.
 

jayel

Active Member
Licensed User
Longtime User
ok found it

B4X:
ABHelper.Initialize ' ABHelper was dimmed as ACActionBar
    ABHelper.ShowUpIndicator = True '

how about the click event ?
Somebody?
 

corwin42

Expert
Licensed User
Longtime User

shashkiranr

Active Member
Licensed User
Longtime User
Hi @corwin42 and All,

Is is possible to add spinner in the actionbar. I want to add an action menu and when clicked a spinner should appear below.

Regard,
SK
 

corwin42

Expert
Licensed User
Longtime User
Hi @corwin42Is is possible to add spinner in the actionbar. I want to add an action menu and when clicked a spinner should appear below.

Not in the Actionbar but you can use a Toolbar for this.
 

corwin42

Expert
Licensed User
Longtime User
I have completely rewritten this tutorial and updated the example project.

It now uses the inline Java code feature and it is not anymore a "hack" with side effects. Much simpler to use than the solution before.

Still it depends on the AppCompat library.

Enjoy it.
 
Last edited:

andre.astafieff

Member
Licensed User
Longtime User
Hi everyone,

I thought it would be the best idea to post here since this is the most recent thread that is dedicated to Appcompat, so my apologies if it's not where it belongs.

I'm currently trying to get Appcompat to work, it's crashing every time I debug and I'm not getting any errors in the logs. I have updated all libraries correctly (I think) and added all additionalRes. Whenever I start debugging, install and open the app it just crashes right afterwards. At first I thought it was a problem with my project, but then I tried it with the example provided with the original AppCompat thread and it crashed as well. I'm currently on B4a version 4.30. Could someone help me with this please? Any help will be greatly appreciated.

I also made a video demonstrating the appcrash since it doesn't show anything in the logfile so I hope that helps...

Video:


Managed to find the problem? The same is happening in my tests using a Galaxy E5 or Motorola Razr D1 ...
 
Top