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:

MarcoRome

Expert
Licensed User
Longtime User
I believe the 80%y should be 100%y here

Hi DonManfred i try in this mode but without result.
But with this code:

B4X:
Sub bar_TabChanged(Index As Int, STab As StdTab)
    Activity.RemoveAllViews
    Dim pnl As Panel = STab.Tag
    pnl.Color = Colors.Black
    Dim height As Int
    If 100%y > 100%x Then
        height = 100%y - 48dip
    Else
        height = 90%y
    End If
    Activity.AddView(pnl, 0, 0, 100%x, 90%y)
    Activity.AddView(AdView1, 0dip, 40%y, 100%x, height) ' <------------- 40%y ??? 
    AdView1.LoadAd
    If pnl.NumberOfViews = 0 Then
        pnl.LoadLayout(Index)
    End If
End Sub

i have this effect with two device different ( Huawei ):

Screenshot_2015-03-01-09-04-58.png



and Samsung:

2015-03-01 08.14.16.png


Work .... but the question is ... why 40%y ???
o_O
 

DonManfred

Expert
Licensed User
Longtime User
What let you think the height is 40%y?
Dim height As Int
If 100%y > 100%x Then
height = 100%y - 48dip
Else
height = 90%y
End If

So height is either 100%y-48dip OR 90%y. On this screenshot the height is 100%y-48dip and 48dip is the height of the actionbar if i remember correctly.
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
What let you think the height is 40%y?


So height is either 100%y-48dip OR 90%y. On this screenshot the height is 100%y-48dip

yes... but explaining better, if i put this :

B4X:
Sub bar_TabChanged(Index As Int, STab As StdTab)
    Activity.RemoveAllViews
    Dim pnl As Panel = STab.Tag
    pnl.Color = Colors.Black
    Dim height As Int
   
    Activity.AddView(pnl, 0, 0, 100%x, 90%y)
    Activity.AddView(AdView1, 0dip, 40%y, 100%x, 90%y) '<------ 40%y ???

    AdView1.LoadAd
    If pnl.NumberOfViews = 0 Then
        pnl.LoadLayout(Index)
    End If
End Sub

... but why if i put Top = 40%y work ??
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
how can I change the background color for each SlideBar.Tab?
Is there an event: SlidePage_click (Position as int)?
 

achtrade

Active Member
Licensed User
Longtime User
Try this:
B4X:
Dim jo As JavaObject = ActionBar1
jo = jo.GetField("bar")
jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(true))
jo.RunMethod("setCustomView", Array As Object(<ViewYouWantToAdd>))

I added a label using that code, How can add second view like a button ?

I tried this without luck:
B4X:
jo.RunMethod("setCustomView", Array As Object(label1, button1))

and this
B4X:
Dim jo2 As JavaObject = ActionBar1
jo2 = jo2.GetField("bar")
jo2.RunMethod("setDisplayShowCustomEnabled", Array As Object(true))
jo2.RunMethod("setCustomView", Array As Object(button2))

thanks.
 

achtrade

Active Member
Licensed User
Longtime User
Add a panel and then add the two views to this panel.

I did this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
bar.Initialize("bar")
    lblTitle.Initialize("")
    pnlBar.Initialize("")
    btnOnline.Initialize("btnOnline")

    Dim jo As JavaObject = bar
    jo = jo.GetField("bar")
    jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(True))
    jo.RunMethod("setCustomView", Array As Object(pnlBar))
 
    pnlBar.AddView(lblTitle, pnlBar.left ,pnlBar.Top, 50dip, 30dip)
    pnlBar.AddView(btnOnline, lblTitle.Width ,pnlBar.Top , 50dip, 30dip)
end sub

and I got this message:
Error occurred
an error has occurred in sub
java.lang.classcastexception:
android.app.ActionBar$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams


Note: I fixed it with this:

B4X:
pnlBar.Width = 100%x
pnlBar.AddView(lblTitle, 0 , 20dip, 150dip, pnlBar.Height)
pnlBar.AddView(btnOnline, lblTitle.Width, 0, 100dip, pnlBar.Height)
 
Last edited:

Tayfur

Well-Known Member
Licensed User
Longtime User
hello
sliding tabhost dont work without "title".
I recive an error when "#IncludeTitle: false"
can it work without title? it is possible?

B4X:
#Region  Activity Attributes
    #FullScreen: True
    #IncludeTitle: false
#End Region

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim height As Int = CalculateHeight(True, True)
    vp.Initialize("vp", 3, 100%x, height)
    Activity.AddView(vp.AsView, 0, 0, 100%x, height)
    'load the pages layouts
    vp.Panels(0).LoadLayout("0")
    vp.Panels(1).LoadLayout("1")
    vp.Panels(2).LoadLayout("2")
    bar.Initialize("bar")
    'bar.Icon = LoadBitmap(File.DirAssets, "giris.png")
    bar.NavigationMode = bar.NAVIGATION_MODE_TABS ' <<< line 36
    bar.AddTab("Aşama 1")
    bar.AddTab("sekeme 2")
    bar.AddTab("fimal 3")
    'bar.ShowUpIndicator = False' True
    'bar.SelectedIndex = currentPage
    'Activity.Title = "bu ana başlık"
    'bar.Subtitle = "alt başlıkta bu"
   
    Activity.AddMenuItem3("", "mnuEdit", LoadBitmap(File.DirAssets, "ic_action_edit.png"), True)
    Activity.AddMenuItem3("", "mnuNew", LoadBitmap(File.DirAssets, "ic_action_new.png"), True)
    Activity.AddMenuItem3("", "mnuUndo", LoadBitmap(File.DirAssets, "ic_action_undo.png"), True)
    Activity.AddMenuItem3("Home", "ActionHome", LoadBitmap(File.DirAssets, "ic_action_undo.png"), True)
    Activity.AddMenuItem3("Search", "ActionSearch", LoadBitmap(File.DirAssets, "ic_action_undo.png"), True)
End Sub


Error is
B4X:
Installing file.
PackageAdded: package:b4a.example
** Activity (main) Create, isFirst = true **
480 x 800, scale = 1.5 (240 dpi)
Error occurred on line: 36 (Main)
java.lang.NullPointerException
    at anywheresoftware.b4a.objects.StdActionBar.setNavigationMode(StdActionBar.java:209)
    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:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at b4a.example.main.afterFirstLayout(main.java:100)
    at b4a.example.main.access$100(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    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:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)
 

Reids

Member
Licensed User
Longtime User
It appear the actionbar doesn't work with the appcompact
I need to make slider tab like this, but after changing the manifest file to appcompact themes some properties doesn't work correctly so error will poping out, it need <ics themes to run
If I use old ics themes then force the "material drawer" library to work, the drawer appear below the tab, not overide all of view :v
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
greetings

I have two tab in one database to another map ( OSM ) .
I NEED TO USE THE FORM DOES NOT WORK SLIDE PANELS ("NO SLIDE FORM") BY MAKING OSMDROID MAP HANDLING DIFFICULT ...
BUT WHEN CHANGE TO ANOTHER TAB (DATABASE) STOPS WORKING THE MAP . I tried CALL AGAIN THE MAP BUT NOT WORK .
One way to create TAB NOT REMOVE THE VIEWS OF A TAB TO CHANGE BUT NOT HAVE SLIDE FUNCTION.

"NO SLIDE FORM"
......................................

bar.Initialize("bar")
bar.NavigationMode = bar.NAVIGATION_MODE_TABS 'change to list to see the other mode
'bar.SetListItems(Array As String("Dropdown 1", "Dropdown 2", "Dropdown 3"))
bar.Subtitle = "VER 1.2.2"
Activity.Title = "CAP-USO"
Panell1.Initialize("1")
panell2.Initialize("2")

bar.AddTab("DATOS").Tag = panell2
bar.AddTab("GIS").Tag = Panell1

Panell1.LoadLayout("1")
panell2.LoadLayout("2")
..........................................

"SLIDE FORM"
..........................................
vp.Initialize("vp", 2, 100%x,100%Y)
Activity.AddView(vp.AsView, 0, 0, 100%x,100%Y)
vp.Panels(0).LoadLayout("2")
vp.Panels(1).LoadLayout("1")
Var.Initialize("bar")
bar.NavigationMode = bar.NAVIGATION_MODE_TABS
bar.AddTab("DATOS")
bar.AddTab("GIS")
bar.SelectedIndex = currentPage
bar.Subtitle = "VER:1.2"
Activity.Title = "CAP-USO"
...............................................
 

katrad

Member
Licensed User
Longtime User
I'm having a problem. I have a stdactionbar and when the search icon is clicked, I put in a custombar on top. But, it's offset. I've tried to explicitly set top and left, but, can't. Does anyone know why this happens. (see screens shot below). Here is the code when the search icon is clicked:

B4X:
Sub Search_Click
    Log("SearchClicked")
    Dim jo As JavaObject = bar
    '
    Activity.Title = ""
    bar.Icon = Null
    bar.ShowUpIndicator = False
    bar.Subtitle = ""
    '
    SchPanel.Initialize("SchPanel")
    SchPanel.LoadLayout("SchPanel")
    '
    jo = jo.GetField("bar")
    jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(True))
    jo.RunMethod("setCustomView", Array As Object(SchPanel))
    If isFilterOn Then StatLabel.Text = FilterOn Else StatLabel.Text = FilterOff
End Sub
 

Attachments

  • picreg.png
    picreg.png
    22.4 KB · Views: 258
  • piccust.png
    piccust.png
    15.8 KB · Views: 260

katrad

Member
Licensed User
Longtime User
Thank you very much for the quick reply. I'm getting the same result. It's like it's keeping a place for the upindicator? Here is my updated code:

B4X:
Sub Search_Click
    Log("SearchClicked")
    Dim jo As JavaObject = bar
    '
    Activity.Title = ""
    bar.Icon = Null
    bar.ShowUpIndicator = False
    bar.Subtitle = ""
    '
    pnlBaseSch.Initialize("pnlBase")
    pnlBaseSch.LoadLayout("SchPanel")
    '
    pnlSchPanel.Initialize("pnlSchPanel")
    pnlSchPanel.AddView(pnlBaseSch, 0,0,100%y, 56dip)
    '
    jo = jo.GetField("bar")
    jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(True))
    jo.RunMethod("setCustomView", Array As Object(pnlSchPanel))
    If isFilterOn Then StatLabel.Text = FilterOn Else StatLabel.Text = FilterOff
End Sub
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
=V1.52 is released. Fixes an incompatibility with Android 5.0. Note that the ButtonClicked event will not work on these devices. An alternative way to add this event will be added in the next version of B4A.

hello Erel,
There are new for this fix?
thanks
 
Top