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:

corwin42

Expert
Licensed User
Longtime User

bluedude

Well-Known Member
Licensed User
Longtime User
It seems to be a problem indeed. Funny thing is, Google uses it in Google Play UI. Thumbs down for them.

So I guess I need to use something else. I like the sliding UI and it makes a lot of sense to move between screens. Even babies know how to use it :)
 

corwin42

Expert
Licensed User
Longtime User
It seems to be a problem indeed. Funny thing is, Google uses it in Google Play UI. Thumbs down for them.

So I guess I need to use something else. I like the sliding UI and it makes a lot of sense to move between screens. Even babies know how to use it :)
Try AHViewPager with fixed Tab indicator or use a manually added tabhost
 

sule

Member
Licensed User
Longtime User
Is it possible to use this library(AHQuickAction3D) with Erels StdActionBar to popup menu from StdActionBar menu
 

bluedude

Well-Known Member
Licensed User
Longtime User
corwin,

I actually always used AHViewPager bit wanted to replace it by something more simple. I think we will have the same problem because it is also based on StdViewPager isn't it?

Cheers.
 

corwin42

Expert
Licensed User
Longtime User
corwin,

I actually always used AHViewPager bit wanted to replace it by something more simple. I think we will have the same problem because it is also based on StdViewPager isn't it?

Cheers.

With AHViewpager there is a fixed tab indicator you can use instead of the standard ActionBar tabs.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Tried it, for some reason I cannot get it to work. I also want to use the pagertabs or fixed.

barDrawer.ContentPanel.AddView (pagerView,0,0,100%x,100%y)

PagerView is the AHViewPager, either fixed or not.
 

yappa

Member
Licensed User
Longtime User
Is it possible to display android circle spinner (progressbar with "indeterminate") instead of icon to indicate progress?
Thanks!
 

woniol

Active Member
Licensed User
Longtime User
Is it possible to lock the ability to change tabs with a swipe gesture (ViewPager)?
I load the views from server and would like to keep swipe locket until i get all data to buld the views.
 

Inman

Well-Known Member
Licensed User
Longtime User
Is it possible to update or change the actionbar icon? My app is basically for an online shop. So one of my actionbar icons is the shopping cart icon. I would like to show the normal icon while the cart is empty and a number on top of the icon, based on the number of items in the cart (like the screenshot). Can we do this?

h5XZZuH.jpg
 

Penguin

Member
Licensed User
Longtime User
The Library returns "java.lang.NoSuchFieldException:mActionView" in the L Developer Preview

Is there any way to fix it?
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Very Nice Library.

Have build an B4A Offline Reader for the B4J HowTos based upon this library. Download open source here.
This app makes use of a SQLite database, Json to keep the settings, Dialogs to filter, HTTPUtils2 to update the database from server.

Note: Still some tasks to do (like update menu, preferences) - for the latest version see B4J HowTos.

Enjoy.
 
Last edited:

Bryan

Member
Licensed User
Longtime User
I am too having a similar problem with properties of a spinner on the stdactionbar. Particularly, setting left position of the spinner. Same as in post #56. Width of the spinner sets fine. It's width size adjust to the number of characters correctly. I also tried using a panel with the spinner on it and received the same errors. Should I maybe use reflection to do this or will it give the same result?
Thank you for any help it is greatly appreciated.
Bryan

B4X:
Sub Globals
    Dim bar As StdActionBar
    Dim spin As Spinner
    Dim jo As JavaObject = bar
End Sub

Sub Activity_Create(FirstTime As Boolean)
    bar.Initialize("bar")
    spin.Initialize("")
        spin.AddAll(Array As String("One", "Two", "Three", "Four", "Five" , "Six")) 
   
    jo = jo.GetField("bar")
    jo.RunMethod("setDisplayShowCustomEnabled", Array As Object(True))
    jo.RunMethod("setCustomView", Array As Object(spin))
    spin.Width = -2
    spin.Left = 20dip
 
End Sub


B4X:
** Activity (main) Create, isFirst = true **

java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
    at anywheresoftware.b4a.objects.ViewWrapper.setLeft(ViewWrapper.java:158)
    at b4a.example.main._activity_create(main.java:333)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at b4a.example.main.afterFirstLayout(main.java:98)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
~e:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)


    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.ClassCastException: android.app.ActionBar$LayoutParams cannot be cast to anywheresoftware.b4a.BALayout$LayoutParams
 
Top