Android Code Snippet Remove Material Action Bar Shadow

Erel

Administrator
Staff member
Licensed User
The material action bar is elevated by default. The following code sets the elevation to 0 and removes the shadow.

B4X:
Sub RemoveActionBarShadow
   Dim p As Phone
   If p.SdkVersion >= 21 Then
     Dim jo As JavaObject
     jo.InitializeContext
     Dim elevation As Float = 0
     jo.RunMethodJO("getActionBar", Null).RunMethod("setElevation", Array(elevation))
   End If
End Sub
Depends on Phone and JavaObject libraries.

upload_2016-6-7_8-23-14.png
 

DonManfred

Expert
Licensed User
Where do i need to call this method?
I tried it in activity_create and _resume.

** Activity (main) Create, isFirst = true **
Iconics.init()
** Activity (main) Resume **
main_removeactionbarshadow (java line: 648)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
at de.donmanfred.MaterialDrawer.main._removeactionbarshadow(main.java:648)
at de.donmanfred.MaterialDrawer.main._activity_resume(main.java:492)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at de.donmanfred.MaterialDrawer.main.afterFirstLayout(main.java:108)
at de.donmanfred.MaterialDrawer.main.access$000(main.java:17)
at de.donmanfred.MaterialDrawer.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
main_removeactionbarshadow (java line: 648)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4j.object.JavaObject.getCurrentClass(JavaObject.java:258)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:118)
at de.donmanfred.MaterialDrawer.main._removeactionbarshadow(main.java:648)
at de.donmanfred.MaterialDrawer.main._activity_resume(main.java:492)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at de.donmanfred.MaterialDrawer.main$ResumeMessage.run(main.java:298)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.RuntimeException: Object should first be initialized (JavaObject).
 

Erel

Administrator
Staff member
Licensed User
Create a new project with the following code and try it:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   RemoveActionBarShadow
End Sub

Sub RemoveActionBarShadow
  Dim p As Phone
  If p.SdkVersion >= 21 Then
  Dim jo As JavaObject
  jo.InitializeContext
  Dim elevation As Float = 0
  jo.RunMethodJO("getActionBar", Null).RunMethod("setElevation", Array(elevation))
  End If
End Sub
 

trueboss323

Active Member
Licensed User
If I want to use this code on an Android version older than Lollipop, can I change SdkVerion to 14 ?

EDIT: Okay on my device running 5.1 , I am suddenly getting this error: java.lang.RuntimeException: Object should first be initialized (JavaObject).
 
Last edited:

trueboss323

Active Member
Licensed User
It does not happen when I try it in the new project.
Strange because i remember this code working fine for the first few times before it started giving me an error. It doesn't crash on my Android 4.x device. I am using AppCompact if that helps.
 
Last edited:

corwin42

Expert
Licensed User
Top