Android Code Snippet Remove Material Action Bar Shadow

Discussion in 'Code Snippets' started by Erel, Jun 7, 2016.

  1. Erel

    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.

    Code:
    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
     
  2. DonManfred

    DonManfred Expert Licensed User

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

     
  3. Erel

    Erel Administrator Staff Member Licensed User

    Are you using the material theme? Is the action bar visible?
     
  4. DonManfred

    DonManfred Expert Licensed User

    yes
    yes
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Create a new project with the following code and try it:
    Code:
    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
     
    Peter Simpson likes this.
  6. trueboss323

    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: Jun 8, 2016
  7. Erel

    Erel Administrator Staff Member Licensed User

    Does this error happen with a new project with the code I posted above?
     
  8. trueboss323

    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: Jun 10, 2016
  9. Erel

    Erel Administrator Staff Member Licensed User

    This is probably the reason for this error. There is no native action bar.
     
  10. trueboss323

    trueboss323 Active Member Licensed User

    Is there a workaround I can use for this?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    This code will only work with the native action bar. Please start a new thread for a question about AppCompat action bar.
     
  12. corwin42

    corwin42 Expert Licensed User

    For AppCompat you should use
    "getSupportActionBar" instead of "getActionBar".
     
  13. trueboss323

    trueboss323 Active Member Licensed User

    Thank you corwin42, it is working great now!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice