Android Question Is there any way to reuse AppCompat Actionbar code appwide?

sktanmoy

Active Member
Licensed User
Is there any way to use AppCompact Actionbar in all Activity without writing the same code again and again. I tried to integrate the code into a class (I'm not using XUI). But failed.
 

Erel

Administrator
Staff member
Licensed User
You should never write the same code more than once.

Post an example of the code you want to reuse.
 

sktanmoy

Active Member
Licensed User
@Erel,
I want to use same code (Ref: attached file) for all of the activity as I want to have AppCompact Actionbar in every activity.
 

Attachments

Erel

Administrator
Staff member
Licensed User
Looks to me that you can just move this code to a class and it will work.
 

sktanmoy

Active Member
Licensed User
I tried that also with no success

Here is the menu class
B4X:
Sub Class_Globals
   Private ACToolBarLight1 As ACToolBarLight
   Private ToolbarHelper As ACActionBar
   Private MenuIcon As BitmapDrawable
   Private SMenu As SlidingMenu
   Private MenuItems As ListView
   Private Menus As List
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   'Menu Part
   Dim LeftPanel As Panel = Generate_Menu
   
   Menus.Initialize
   Menus.AddAll(Array As String(Login.Name, "BMR" , "Logout"))
   
   For i=0 To Menus.Size-1
       MenuItems.AddSingleLine(Menus.Get(i))
   Next
   MenuItems.Color = Colors.DarkGray
   
End Sub


Sub ACToolBarLight1_NavigationItemClick
   SMenu.ShowMenu
End Sub


Sub Logout_Click
   Sign_Out
End Sub

Sub MenuItems_ItemClick (Position As Int, Value As Object)
   Dim mnu As String = Menus.Get(Position)
   If mnu = "Logout" Then
       Logout_Click
   End If
   If mnu = "BMR" Then
       StartActivity(BMR)
   End If
   
End Sub

Sub CloseActivities
   Dim jo As JavaObject
   jo.InitializeContext
   jo.RunMethod("finishAffinity", Null)
End Sub

Sub Sign_Out
   CallSub(Backendless, "Sing_Out")
   Login.Google.SignOutFromGoogle
   Login.Facebook.SignOut
   CloseActivities
End Sub


Sub Generate_Menu As Panel
   MenuIcon.Initialize(LoadBitmap(File.DirAssets, "hamburger.png"))
   ToolbarHelper.Initialize
   ToolbarHelper.ShowUpIndicator = True
   
   ACToolBarLight1.NavigationIconDrawable = MenuIcon
   ACToolBarLight1.InitMenuListener
   
   ACToolBarLight1.Title = "BlackOps MacroFlex"
   ACToolBarLight1.SubTitle = ""
   ACToolBarLight1.Color = Colors.DarkGray
   
   SMenu.Initialize("SMenu")
   Dim offset As Int = 100dip
   SMenu.BehindOffset = offset
   SMenu.Mode = SMenu.LEFT
   
   Dim lftMenu As Panel
   lftMenu.Initialize("")
   SMenu.Menu.AddView(lftMenu, 0, 0, 100%x - offset, 100%y)
   lftMenu.LoadLayout("left_menu")
   
   Return lftMenu
End Sub
Here is the error log
B4X:
Error occurred on line: 63 (Menu)
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
   at de.amberhome.objects.appcompat.ACActionBar.setShowUpIndicator(ACActionBar.java:116)
   at com.rong.macroflex.menu._generate_menu(menu.java:165)
   at com.rong.macroflex.menu._initialize(menu.java:60)
   at com.rong.macroflex.bmr._activity_create(bmr.java:375)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
   at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:360)
   at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
   at java.lang.reflect.Method.invoke(Native Method)
   at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
   at com.rong.macroflex.bmr.afterFirstLayout(bmr.java:104)
   at com.rong.macroflex.bmr.access$000(bmr.java:17)
   at com.rong.macroflex.bmr$WaitForLayout.run(bmr.java:82)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6186)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
** Activity (bmr) Resume **

On line 63, in Menu class, there is ``ToolbarHelper.ShowUpIndicator = True``.

I had no idea how to deal with this one.
 

Erel

Administrator
Staff member
Licensed User
Please make a small project out of this and upload it.
 
Top