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,190
  • StdActionBar.zip
    22.9 KB · Views: 1,061
Last edited:

sigster

Active Member
Licensed User
Longtime User
Thanks Erel
I try the " Android Action Bar Style Generator" I can change the background but the text color in the bar is black
 

miguelconde

Member
Licensed User
Longtime User
does the stdActionBar need some dependency?, Because i had this errror when i add it.
A referenced library is missing: android-support-v4

I can see the lib fine in the libs tab, (StdActionBar (version: 1.52)
But when i compiled it i have the error.

Thank
 

MaFu

Well-Known Member
Licensed User
Longtime User
Search for the latest android-support-v4.jar in your android sdk tree (normally found in sdk-path\extras\android\compatibility\v4) and copy it in your additional libs folder.
 

corwin42

Expert
Licensed User
Longtime User
does the stdActionBar need some dependency?, Because i had this errror when i add it.
A referenced library is missing: android-support-v4

I can see the lib fine in the libs tab, (StdActionBar (version: 1.52)
But when i compiled it i have the error.

The StdActionbar does not have such a dependency. You must have another library selected which needs the support library.
 

miguelconde

Member
Licensed User
Longtime User
I just download the sample project, I opened and compiled. I got that error.

Parsing code. 0.00
Compiling code. 0.05
Compiling layouts code. 0.02
Generating R file. 1.26
Compiling generated Java code. 1.83
Convert byte code - optimized dex. Error
A referenced library is missing: android-support-v4
 

Wembly

Member
Licensed User
Longtime User
A fix for the crash issue will be soon available. The fix for the icon click event will probably not be available before November 3.

@Erel - Please advise if fix available or when will be as my app is now receiving negative reviews & ratings in Google Play as it currently does not work on Android 5.
 
Top