B4A Class ActionDrawer - ActionBar and NavDrawer Made Perfect

edgeryder1000

Member
Licensed User
ActionDrawer is a combination of my own custom actionbar class and the native navigation drawer. It aims to be an extremely simple way for basic4android users to make quality apps which follow google's guidlines, while still allowing lots of customization.
Features include: Dark and light themes, dynamic fitting of the action buttons to the overflow menu(try rotating the demo app), the drawer and up indicators, and more.
ActionDrawer requires AHNavigationDrawer, the reflection library, and the drawables in the zip below.

NOTE: For version 2.0 I will be rewriting this class to make it more flexible and simple, and also adding some big features like pull to refresh, multiple navdrawers, and making it easier to add nice lists to the drawer. v2.0 will be incompatible with the current version, so you may want to wait instead of using the current class.

Update v1.01
- Fixed a bug where double-tapping and holding the home button would cause it to stay selected
v1.1
- Added tooltips! When the actionbar buttons are long-pressed a special toast message will be displayed
- fixed a title-shortening bug
v1.2
-Changed how click events work (you now specify the eventname when adding the button) as suggested by corwin42. This means that v1.2 is not fully compatible with previous versions
v1.21
-Made SectionTitle (string) and lTitle (label) public. Now you can change the actionbar title however you want

Screenshot_2013-08-24-12-05-32.png
 

Attachments

Last edited:

admac231

Active Member
Licensed User
Unfortunately the app I'm working on doesn't really have a use for the ActionDrawer. I was just looking for an easy to implement Actionbar for just now.

The code is very easy to follow. In fact, I added a loading animation and a few methods to turn it on and off very easily. Again, thank you.
 

edgeryder1000

Member
Licensed User
If I understand your question correctly, then what you need to do is add the views using AD.NavDrawer.ContentPanel.AddView. I only just realised you need to do this when you use AHNavigationDrawer, so I will add a simpler way to do it in the class today. Thanks for reminding me
 

shashkiranr

Active Member
Licensed User
:) i just realized it after i posted the question and have already modified my code. Still it will help others also. I will wait for your update.

Regards,
SK
 

edgeryder1000

Member
Licensed User
If you just want an event when the home button (button with the icon and title) is clicked first delete lines 443-445, then add
B4X:
Else
       CallSubDelayed(Main, "Home_Click")
at line 440 in the class module. Then if you have set the indicator to "none" a sub called Home_Click will be called, which you will have to create in the main module. If you set the indicator to "Up" then the sub Up_Click can be used to handle the click. If the indicator is "Drawer" then pressing the home button only opens the drawer.
 

ciprian

Active Member
Licensed User
Hi there. I'm having some trouble with a ToogleButton.
I have added a toggle button into the content container.
B4X:
AD.NavDrawer.ContentPanel.AddView(sw, 80dip, 300dip, 200dip, 50dip)
And the sub
B4X:
Sub sw_CheckedChange(Checked As Boolean)
    If Checked Then
        Log("OK")
    Else
        Log("NOK")
    End If
End Sub
The problem is that the checked property is not working well. Cant see the logs, is simulator seems to work, but the logs not. It seems that it is not fireing.
 
Last edited:

Rock

New Member
Licensed User
When I start your sample application I get the following error: "An Error has occurred in sub: java.lang.Exception: Sub not found. Continue?" I'm not sure what sub is being called. Also do you know how I would integrate the AHPreferenceActivity with the settings overflow option?

Thanks,

Mat
 

edgeryder1000

Member
Licensed User
When I start your sample application I get the following error: "An Error has occurred in sub: java.lang.Exception: Sub not found. Continue?" I'm not sure what sub is being called. Also do you know how I would integrate the AHPreferenceActivity with the settings overflow option?
Thanks for the heads-up! I have updated the file, but if you want to fix it yourself just add quotes around "ShadowSendToBack" on line 265 in the class.

I haven't used AHPreferenceActivity before, but I think I'll try it out now :) If you want to open the settings activity when the user presses the overflow button and selects "Settings" use this code in Activity_Create:
B4X:
Dim OverflowItems As List
    OverflowItems.Initialize
    OverflowItems.Add("Settings")
And add this sub to the main activity
B4X:
Sub Overflow_Click(Item As String)
    Select Item
        Case "Settings"
            StartActivity(AHPrefereceActivity.CreateIntent)
    End Select
End Sub
In the demo this is already set up, you just need to put your code in Overflow_Click --> Case "Settings"
 

edgeryder1000

Member
Licensed User
Hi there. I'm having some trouble with a ToogleButton.
I have added a toggle button into the content container.
B4X:
AD.NavDrawer.ContentPanel.AddView(sw, 80dip, 300dip, 200dip, 50dip)
And the sub
B4X:
Sub sw_CheckedChange(Checked As Boolean)
    If Checked Then
        Log("OK")
    Else
        Log("NOK")
    End If
End Sub
The problem is that the checked property is not working well. Cant see the logs, is simulator seems to work, but the logs not. It seems that it is not fireing.
Have you tried using message boxes some other flag instead of logs? Was the toggle declared and initialised correctly? I don't really know why logging isn't working for you. Sorry.
 

corwin42

Expert
Licensed User
Really nice class.

You should add an EventName parameter to the AddAction subs and not use the displayed item text as event name. Currently it is not possible to translate apps with your ActionDrawer.
Same with the overflow menu. You can add special overflow item types to the list with eventname and displayed text.

I don't really understand what you are doing in the onTouch events. Do you just change color of the item so it looks pressed? Why don't you use a StateListDrawable as the background for normal ImageViews for the action items? Should work and would make it much easier.
 

edgeryder1000

Member
Licensed User
Thanks Markus, you're pretty much my favourite b4a member (along with Erel and thedesolatesoul)

Your ideas make a lot of sense, I will add them in soon. With the touch events, when I was trying to replicate the native actionbar with statelist drawables I couldn't deselect the button when the user moved their finger off of it. I think that was the only reason, I'm just a bit obsessive about things like that.

Edit: Updated the class to handle events from actionbar buttons the way you said.
 
Last edited:

shashkiranr

Active Member
Licensed User
Hi Edgeryder,

In a scenario where i need only delete button on the action bar. I am able to achieve this by using "None" value during initialization and by removing the icon , title and rest all buttons.

What i am not getting is the delete button should be in the right corner of action bar(in place of overflow button) but it is coming in the middle.

Kindly let me know how i can achieve this.

Regards,
SK
 

edgeryder1000

Member
Licensed User
ActionDrawer wasn't designed to be extremely customizable, so to do this you need to edit the class itself. I had a spare minute, so here is a modified version which, fingers-crossed, will work for you. The only difference is the addition of Initialize2, which replaces the overflow button with a standard actionbar button. Hope this helps you
 

Attachments

shashkiranr

Active Member
Licensed User
Hi Edgeryder,

I am getting object not initialised error in the line initialize2 in actiondrawerplus. Can you tell me the mistake im making

B4X:
** Activity (main) Resume **
Installing file.
Installing file.
PackageAdded: package:actn.drwr
** Activity (main) Create, isFirst = true **
actiondrawerplus_initialize2 (B4A line: 38)
AD.Initialize2("ic_delete.png","Delete_Click",True,Colors.RGB(48,48,48),"Dark",True,True,False,Activity,Me)
java.lang.RuntimeException: Object was not initialized.
    at anywheresoftware.b4a.debug.Debug.PushSubsStack(Debug.java:117)
    at actn.drwr.actiondrawerplus._initialize2(actiondrawerplus.java:1939)
    at actn.drwr.main._activity_create(main.java:264)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at actn.drwr.main.afterFirstLayout(main.java:89)
    at actn.drwr.main.access$100(main.java:16)
    at actn.drwr.main$WaitForLayout.run(main.java:74)
    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:4898)
    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:1008)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object was not initialized.
** Activity (main) Resume **
 

edgeryder1000

Member
Licensed User
Sorry, I think I stuffed up there. I didn't get a chance to test it the first time (still don't know how to fix it). So I made a new class that can be used in conjunction with ActionDrawer to solve your problem (in the zip). Sorry for the inconvenience, and thanks for your input. Now I feel motivated to add in a proper contextual actionbar.
 

Attachments

Top