B4A Library StdActionBar - Another ActionBar library

StdActionBar (Standard ActionBar) library is based on the native ActionBar API, therefore it is only supported by Android 4+.

StdActionBar / StdViewPager tutorial: ActionBar / Sliding Pages tutorial

The share of Android 2.x devices is dropping and currently (January 2014) it is only 24%.

This library allows you to add tabs and dropdown list to the action bar:

upload_2014-1-16_17-20-37.png
upload_2014-1-16_17-21-7.png


The attached example demonstrates both modes. You should change the navigation mode to see the two modes.

List mode

Adding a list is very simple. You set the NavigationMode:
B4X:
bar.NavigationMode = bar.NAVIGATION_MODE_LIST
Add the items:
B4X:
bar.SetListItems(Array As String("Dropdown 1", "Dropdown 2", "Dropdown 3"))

And handle the event:
B4X:
Sub bar_ListItemSelected (Index As Int, Value As String)
   Log("Selected value: " & Value)
End Sub

Tabs mode

Unlike TabHost, the tabs do not hold any views. You are responsible for switching the layout based on the selected tab.

The first step is to set the navigation mode:
B4X:
bar.NavigationMode = bar.NAVIGATION_MODE_TABS

Then we add the tabs:
B4X:
bar.AddTab("Tab 1")
bar.AddTab returns a StdTab object. We can use it to modify the tabs:
B4X:
bar.AddTab("Tab 1").Tag = panel1
bar.AddTab("Tab 2").Tag = panel2
'Add icon to tab 3
Dim tb As StdTab = bar.AddTab("Tab 3")
tb.Tag = panel3
Dim bd As BitmapDrawable
bd.Initialize(LoadBitmap(File.DirAssets, "ic_small.png"))
tb.Icon = bd
In the above code we add three tabs and use the tabs tag property to store a panel in each tab. Later we will use these panels to switch the layout.

When the TabChanged event is raised we clear the current layout and show the new layout.
B4X:
Sub bar_TabChanged(Index As Int, STab As StdTab)
   Activity.RemoveAllViews
   Dim pnl As Panel = STab.Tag
   Dim height As Int
   If 100%y > 100%x Then
     height = 100%y - 48dip 'fix for the additional tabs height
   Else
     height = 100%y
   End If
   Activity.AddView(pnl, 0, 0, 100%x, height)
   If pnl.NumberOfViews = 0 Then
     pnl.LoadLayout(Index)
   End If
End Sub

Note that the attached example uses the Holo.Light theme. This is done with this manifest editor line:
B4X:
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")

V1.52 is released. Fixes an incompatibility with Android 5.0. Note that the ButtonClicked event will not work on these devices.

You should use Activity_ActionBarHomeClick event instead.
 

Attachments

  • StdActionBarExample.zip
    11 KB · Views: 2,199
  • StdActionBar.zip
    22.9 KB · Views: 1,076
Last edited:

woniol

Active Member
Licensed User
Longtime User
Looks like this problem. Do you think there is something you can do about it?
 

andrewj

Active Member
Licensed User
Longtime User
Hi,
Does this support the model where the action bar has a couple of visible items, and the rest go to a drop-down menu from three dots in the top right corner?
Thanks
Andrew
 

metrick

Active Member
Licensed User
Longtime User
I have a problem on line
B4X:
bar.Initialize("bar")
bar.NavigationMode =  bar.NAVIGATION_MODE_TABS  '<- this is line 78 copied from Erel Example codes.
and here is the logs:
Error occurred on line: 78 (owneradmin)
java.lang.NullPointerException
at anywheresoftware.b4a.objects.StdActionBar.setNavigationMode(StdActionBar.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at com.memoryforever.metsoft.owneradmin.afterFirstLayout(owneradmin.java:98)
at com.memoryforever.metsoft.owneradmin.access$100(owneradmin.java:16)
at com.memoryforever.metsoft.owneradmin$WaitForLayout.run(owneradmin.java:76)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5171)
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:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
at dalvik.system.NativeStart.main(Native Method)
** Activity (main) Resume **
--------- beginning of /dev/log/system
 

metrick

Active Member
Licensed User
Longtime User
Can you upload your project or a subset of it that reproduces this issue?
I have copy and paste the working codes sample from page 1 include sample layouts 0, 1, 2 and commented out all my codes. I still get the same problems.

I have create a new module with new name copy and paste the code to the new module and it's working now...
 
Last edited:

wy328

Member
Licensed User
Longtime User
great work! waiting so long for this kind of lib, a real native action bar lib! thanks!
 

barx

Well-Known Member
Licensed User
Longtime User
OK I'm having a n00b moment. I have this:

B4X:
    ActionBar.Initialize("ActionBar")
   
    Activity.AddMenuItem3("Home", "ActionHome", LoadBitmap(File.DirAssets, "home_icon.png"), True)
    Activity.AddMenuItem3("Search", "ActionSearch", LoadBitmap(File.DirAssets, "search_icon.png"), True)

and the to catch the menu item presses I have (only home so far)

B4X:
Sub ActionHome_ButtonClicked
    Log("click")
    wvContent.LoadUrl(HomeURL)
End Sub

The click is there just to see if the event is triggering. Nothing is happening. What am I missing? lol
 

Augustinas Impalis

Member
Licensed User
Longtime User
How do i add another tab? i have created another layout and did what i think i needed to do which was Addtab and LoadLayout but i keep getting this message "java.lang.ArrayIndexOutOfBoundsException: length=3; index=3" whats missing?
 

Wembly

Member
Licensed User
Longtime User
Hi Erel,

I've been struggling to understand how using the Google link & manifest editor to change the background colour of the action bar and/or split action bar.

Please could you or anyone else who has the knowledge be generous to post an example how to do this? I think this will be of great benefit to many users of this action bar library.

I can change the theme of the action bar from holo light to holo dark but want to be able to specify my own colours.

B4X:
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light.DarkActionBar")

Thanks
 

jcesar

Active Member
Licensed User
Longtime User
To customize the actionbar color and others properties you can use the Android ActionBar style Generator to create your actionbar layouts, generate the ressources files, import into your b4a project and change the android manifest to use your new theme.

Works for me.
 

Wembly

Member
Licensed User
Longtime User
Jcesar - can you please confirm exactly how you 'import' the resource files.

I've used the ActionBar Style Generator as suggested copied the various drawable folders to 'res' folder and set them to read-only.

copied the .xml from the values folder in the zip file - renamed to themes.xml and placed it in the res\xml folder as read-only too.

UPDATE ALL WORKING NOW - FIX - themes.xml should be in folder res\values

I called the theme 'Test' in the generator so in the manifest editor I entered the following:

B4X:
SetApplicationAttribute(android:theme, "@style/Theme.Test")

But I'm getting the error :

AndroidManifest.xml:14: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Test').

Please can you point me in the right direction?

Cheers
 
Last edited:

jcesar

Active Member
Licensed User
Longtime User
Jcesar - can you please confirm exactly how you 'import' the resource files.

I've used the ActionBar Style Generator as suggested copied the various drawable folders to 'res' folder and set them to read-only.

copied the .xml from the values folder in the zip file - renamed to themes.xml and placed it in the res\xml folder as read-only too.

UPDATE ALL WORKING NOW - FIX - themes.xml should be in folder res\values

I called the theme 'Test' in the generator so in the manifest editor I entered the following:

B4X:
SetApplicationAttribute(android:theme, "@style/Theme.Test")

But I'm getting the error :

AndroidManifest.xml:14: error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Test').

Please can you point me in the right direction?

Cheers

It's working ?
 
Top