B4A Library [Class] ActionBar

Hi,

This class allows to create Action Bars.

There was already a good library to do that (AHActionBar) but I needed more features and more flexibility. So here is my version.

Tutorials: How they do #2, How they do #3

Classic.png Menu.png Tab.png Ribbon.png
DragAndDrop.png

v1.1:
Fixed a bug in RemoveButton
Added ReplacePressedDrawable
Fixed a bug in the demo

v1.2:
New functions:
- ReplaceDividerDrawable(Drawable As Object)
- SetIconAsWideAsText(Enabled As Boolean)
Updated the demos

v1.3:
Fixed a bug in ResizeButton (the text height was not correctly computed)
New function: FillParent
Removed the limitation of SetFixedWidth (there's no longer minimum width)

v1.4:
Optimized the loading of drawables
Added the Drag&Drop feature
New functions:
- getLeftPosition(Btn As View) As Int
- getRightPosition(Btn As View) As Int
- StartDragAndDrop(Btn As View, ViewToBlock As Object, OnAfterDropSub As String)
- AbortDragAndDrop
- MoveButtonTo(Btn As View, NewPosition As Int)
Renamed ID to Btn in function parameters

v1.41:
Fixed an issue with JellyBean.
Fixed a visual bug with successive Drag&Drops.

v1.42:
Fixed the computation of the text size of buttons;
No more warnings with B4A v2.7.

v1.43:
Fixed a bug (division by zero).

Enjoy,
Fred
 

Attachments

  • ActionBar v1.43.zip
    62.8 KB · Views: 1,784
Last edited:

lock255

Well-Known Member
Licensed User
Longtime User
I tried your example on different devices with different OS (Gingerbread, ICS, JB). None had a problem with it. The second activity opens without any error.

I was an idiot .. I was convinced that I had the latest version of the library reflection but if you worked, was certainly the library so I went to see if it was the last whisker version and it was not :/

Now it works perfectly, thank you for your support.
 
Last edited:

lock255

Well-Known Member
Licensed User
Longtime User
OK -> it's due to your Reflection library, probably , that is a bit old.

I noticed it when I wrote it here ... and I've updated the post, maybe you were answering before I could save the changes.

While there I was wondering one thing. I would like to put the activity for users to choose the ActionBar and so far it's done. I do not know how to save the changes when a user changes the style. So every time that he has chosen.
Do you have any advice?
 

lock255

Well-Known Member
Licensed User
Longtime User
With your activity can choose the layout of the app, but if I wanted it to be stored in the other activity, so that when the user sets the style they like (including the ones you've made ​​with radio button (classic ect ), is set in the layout of all the activity.
 

Informatix

Expert
Licensed User
Longtime User
With your activity can choose the layout of the app, but if I wanted it to be stored in the other activity, so that when the user sets the style they like (including the ones you've made ​​with radio button (classic ect ), is set in the layout of all the activity.

These styles do not exist as such. They are not defined in the class. They represent different ways to use the class and are therefore entirely created by the user. The fact that you choose a style or another does not change the fact that you will need to call the class in each activity to create the buttons and the bar design. That's why I'm not sure to understand your problem.
If you want to avoid repeating the code in each activity (with a Select to handle the different styles), you can place it in a code module.
 

Harris

Expert
Licensed User
Longtime User
I have noticed when using the ribbon, on my Nexus 7 in landscape, an issue.
If the list of buttons total width is less than the activity width then an empty white space fills to the end of screen.
This is due to the pnlAB set to 100%x in the designer.
To fix this I applied, just before the call to invalidate:

B4X:
pnlAB.Width = AB2.AsPanel.Width ' make parent panel same with as AB
If (Activity.Width - pnlAB.Width) > 1 Then
   pnlAB.Left = (Activity.Width - pnlAB.Width) / 2 
   ' removes extra space and centers the AB
End If

Nice class...
I think I shall use the ribbon AB on top to avoid hitting the back and home key when placed at the bottom. Now I don't have to clutter my forms with (butt ugly) buttons since the HSV takes care of as many as I dare to add.
Thanks!!
 

Informatix

Expert
Licensed User
Longtime User
I have noticed when using the ribbon, on my Nexus 7 in landscape, an issue.
If the list of buttons total width is less than the activity width then an empty white space fills to the end of screen.
This is due to the pnlAB set to 100%x in the designer.
To fix this I applied, just before the call to invalidate:

B4X:
pnlAB.Width = AB2.AsPanel.Width ' make parent panel same with as AB
If (Activity.Width - pnlAB.Width) > 1 Then
   pnlAB.Left = (Activity.Width - pnlAB.Width) / 2 
   ' removes extra space and centers the AB
End If

Nice class...
I think I shall use the ribbon AB on top to avoid hitting the back and home key when placed at the bottom. Now I don't have to clutter my forms with (butt ugly) buttons since the HSV takes care of as many as I dare to add.
Thanks!!

Thank you for the fix.
 

yuhong

Member
Licensed User
Longtime User
OutOfMemoryError

:sign0085:
I have repeatedly run a activty:

clsactionbar_resizebutton (java line: 965)
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)
at .clsactionbar._resizebutton(clsactionbar.java:965)
at.clsactionbar._invalidate(clsactionbar.java:636)
at .clrecord._inittoolsbar(clrecord.java:1239)
at .clrecord._activity_create(clrecord.java:335)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at ..clrecord.afterFirstLayout(clrecord.java:89)
at .clrecord.access$100(clrecord.java:16)
at .clrecord$WaitForLayout.run(clrecord.java:74)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
 

Informatix

Expert
Licensed User
Longtime User
:sign0085:
I have repeatedly run a activty:

clsactionbar_resizebutton (java line: 965)
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)

Java.lang.OutOfMemoryError: bitmap size exceeds VM budget

This error means you ran out of memory. You tried to allocate more than you have at your disposal, probably by using too large bitmaps or too many bitmaps. Without any knowledge of the context, I can't say more. I can just say that has nothing to do with my class.
 

Harris

Expert
Licensed User
Longtime User
Is there a tag property for each AddButton?
This way I could use one sub and perform action based on button selected.
Thanks
 

Informatix

Expert
Licensed User
Longtime User
Is there a tag property for each AddButton?
This way I could use one sub and perform action based on button selected.
Thanks

The Tag property of the AB buttons is reserved for an internal use and cannot be used. But you do not need it to know what button has been clicked. This information is returned by the Click or LongClick event. All buttons can lead to the same event handler if you wish.
 

Harris

Expert
Licensed User
Longtime User
B4X:
Sub Button_Longclick(ActionBar As ClsActionBar, Btn As View)
   Msgbox(" Button "&ActionBar.getLeftPosition(Btn),"Btn Selected")
End Sub

I see. This returns the button's position.
I number my tags (when not using objects) so this works just fine.

Thanks.
 

Informatix

Expert
Licensed User
Longtime User
B4X:
Sub Button_Longclick(ActionBar As ClsActionBar, Btn As View)
   Msgbox(" Button "&ActionBar.getLeftPosition(Btn),"Btn Selected")
End Sub

I see. This returns the button's position.
I number my tags (when not using objects) so this works just fine.

Thanks.

It is better to use their reference:

Dim HomeButton as View
HomeButton = AddButton....

And in _Click:
if Btn = HomeButton then ...
 

Harris

Expert
Licensed User
Longtime User
Yes, works as well - and wont fool you if you drag and drop a button position...

Thanks again.
 

Inman

Well-Known Member
Licensed User
Longtime User
I have only used AHActionbar library till now but it doesn't have an option to set a back button on the left end, very close to the actionbar title (as in the screenshot below). Will it be possible to do it with your class?

9PQJeofDqVayHQqHkxSqgTbTekRLW5ont16sl7yR2bM9mkE4RDef_srXMQWZ4K0EOpM
 

jmon

Well-Known Member
Licensed User
Longtime User
I have only used AHActionbar library till now but it doesn't have an option to set a back button on the left end, very close to the actionbar title (as in the screenshot below). Will it be possible to do it with your class?
The way I do it is by replacing the icon. I have a set of two icons, one with back btn and the other not.

I have a question for Informatrix:

First of all, excellent work and thank you so much for sharing with the community.

I think the class is missing two functions to make it perfect. One would be AB.SetLeftAndRightPadding, to add a little space on the left and right of the actionbar. Now I think the icons are too stuck to the edges of the screen.

The second would be AB.SetIconsSize, to set the icons not to be more than lets set 32dip height and width in an actionbar of 50dip height.

Thanks again.



Sent from my GT-P7500 using Tapatalk HD
 

Informatix

Expert
Licensed User
Longtime User
I think the class is missing two functions to make it perfect. One would be AB.SetLeftAndRightPadding, to add a little space on the left and right of the actionbar. Now I think the icons are too stuck to the edges of the screen.

The second would be AB.SetIconsSize, to set the icons not to be more than lets set 32dip height and width in an actionbar of 50dip height.

Feel free to add SetLeftAndRightPadding to the code and to publish the result here.
About the second function, I'm not sure it's needed. If I remember correctly what I did in this class, you can already set a fixed size for your buttons. And if you want smaller icons, just increase the transparent part around them.
 
Top