Android Code Snippet Remove Material Action Bar Shadow

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.

 

DonManfred

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

 

Erel

B4X founder
Staff member
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime User
Cookies are required to use this site. You must accept them to continue using the site. Learn more…