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

romario87027

Active Member
Licensed User
Longtime User
Compiling code. Error
'System.OutOfMemoryException'.
Following the example downloaded ActionBar.b4a .. while complie error
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
upload_2014-2-16_10-3-38.png


Relevant code:
B4X:
Sub Globals
  Dim mFragment As MapFragment
  Dim gmap As GoogleMap
  Dim MapPanel As Panel
   Dim bar As StdActionBar
   Dim vp As StdViewPager
End Sub

Sub Activity_Create(FirstTime As Boolean)
  MapPanel.Initialize("")
   bar.Initialize("bar")
   bar.NavigationMode = bar.NAVIGATION_MODE_TABS
   bar.AddTab("Map")
   bar.AddTab("Red panel")
   Dim h As Int = CalculateHeight(True, False)
 
   vp.Initialize("vp", 2, 100%x, h)
   MapPanel.Initialize("")
  Activity.AddView(vp.AsView, 0, 0, 100%x, h)
   vp.Panels(0).AddView(MapPanel,0, 0, 100%x, h)
   vp.Panels(1).Color = Colors.Red
  If mFragment.IsGooglePlayServicesAvailable = False Then
  ToastMessageShow("Google Play services not available.", True)
  Else
  mFragment.Initialize("Map", MapPanel)
  End If
End Sub

Sub CalculateHeight (TabsMode As Boolean, SplitEnabled As Boolean) As Int
   If 100%x >= 480dip Then
     Return 100%y
   Else
     Dim fix As Int
     If TabsMode Then fix = 48dip
     If SplitEnabled Then fix = fix + 48dip
     Return 100%y - fix
   End If
End Sub

Sub VP_PageSelected (Position As Int)
   If bar.SelectedIndex <> Position Then bar.SelectedIndex = Position
End Sub

Sub bar_TabChanged(Index As Int, STab As StdTab)
   If vp.currentPage <> Index Then   vp.ScrollTo(Index, False)
End Sub
 

Attachments

  • StdActionBar.zip
    12.4 KB · Views: 279
Last edited:

Augustinas Impalis

Member
Licensed User
Longtime User
View attachment 22721

Relevant code:
B4X:
Sub Globals
  Dim mFragment As MapFragment
  Dim gmap As GoogleMap
  Dim MapPanel As Panel
   Dim bar As StdActionBar
   Dim vp As StdViewPager
End Sub

Sub Activity_Create(FirstTime As Boolean)
  MapPanel.Initialize("")
   bar.Initialize("bar")
   bar.NavigationMode = bar.NAVIGATION_MODE_TABS
   bar.AddTab("Map")
   bar.AddTab("Red panel")
   Dim h As Int = CalculateHeight(True, False)
  
   vp.Initialize("vp", 2, 100%x, h)
   MapPanel.Initialize("")
  Activity.AddView(vp.AsView, 0, 0, 100%x, h)
   vp.Panels(0).AddView(MapPanel,0, 0, 100%x, h)
   vp.Panels(1).Color = Colors.Red
  If mFragment.IsGooglePlayServicesAvailable = False Then
  ToastMessageShow("Google Play services not available.", True)
  Else
  mFragment.Initialize("Map", MapPanel)
  End If
End Sub

Sub CalculateHeight (TabsMode As Boolean, SplitEnabled As Boolean) As Int
   If 100%x >= 480dip Then
     Return 100%y
   Else
     Dim fix As Int
     If TabsMode Then fix = 48dip
     If SplitEnabled Then fix = fix + 48dip
     Return 100%y - fix
   End If
End Sub

Sub VP_PageSelected (Position As Int)
   If bar.SelectedIndex <> Position Then bar.SelectedIndex = Position
End Sub

Sub bar_TabChanged(Index As Int, STab As StdTab)
   If vp.currentPage <> Index Then   vp.ScrollTo(Index, False)
End Sub


Thank you so much Erel!!
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
is it possible to use this library without including Title?
 

figorra

Member
Licensed User
Longtime User
Hi, I have this error on my code... when run.....

B4X:
Parsing code.                          0.00
Compiling code.                        0.02
Compiling layouts code.                0.01
Generating R file.                      0.06
Compiling generated Java code.          Error
javac 1.6.0_45
src\b4a\example\main.java:268: cannot find symbol
symbol  : class StdActionBar
location: package anywheresoftware.b4a.objects
public anywheresoftware.b4a.objects.StdActionBar _bar = null;
                                  ^
1 error
I have de example and the lib......

Can you help me please?
 

jcesar

Active Member
Licensed User
Longtime User
Thanks Erel

it works.

sc_b4a.png


But, is there a way to customize this action view, i need to change some properties like font color, background color, top, fixed width and left position.

I need something like this:

sc_wm.png


This second was created with another tool, and is not a native action bar, its something like a panel with views in b4a.
 

jcesar

Active Member
Licensed User
Longtime User
Have you tried to change the view properties?
Yes

I set up the properties :

B4X:
dim edtTeste as EditText
edtTeste.Initialize("")
edtTeste.Width = 100dip
edtTeste.Hint = "Search !"
edtTeste.TextColor = Colors.White
Dim jo As JavaObject = actionBar
jo = jo.GetField("bar")
jo.RunMethod("setDisplayShowCustomEnabled", ArrayAsObject(true))
jo.RunMethod("setCustomView", ArrayAsObject(edtTeste))

But when i try setup the width or left position i get a java.lang.NullPointerException
 

Dario126

Member
Licensed User
Longtime User
As pointed out, it's only supported by Android 4+.

Can't you (possibly through 'support' library) make it available down to 3+ ?

I'we seen 'StdActionBarHelper' by corwin42, but using that is one more brick on top of another, and usually i'm avoiding that if possible.
 

palmzac

Active Member
Licensed User
Longtime User
Hi Erel,

It's Great library. Where is the detail document ? ( like URL )

How to detect the click event of ic_small.png ? Thanks !
 
Last edited:

corwin42

Expert
Licensed User
Longtime User
As pointed out, it's only supported by Android 4+.

Can't you (possibly through 'support' library) make it available down to 3+ ?

I'we seen 'StdActionBarHelper' by corwin42, but using that is one more brick on top of another, and usually i'm avoiding that if possible.

I managed to use ActionBarCompat from the support library with B4A. It works with some tricks (like modifying the generated Java code in #CustomBuildActions etc.) but it is quite complicated to use and it's a really dirty hack.

I can understand Erel that he (currently) don't want to add support for it because it would make the ActionBar feature quite complicated to use (like adding resource files etc.) and it will need differently generated java code depending on weather ActionBarCompat is used or not. And with a dropping userbase of less than 20% it may be not worth the effort.

Before you ask. Sorry, I won't make my solution public because every new version of B4A may break it totally and I can't do the support for such a dangerous hack.
 
Top