Android Question [SOLVED] Button elevation

Emme Developer

Well-Known Member
Licensed User
Longtime User
In android button elevation is setted by default using statelist. I use this code to delete button shadow

B4X:
Sub RemoveShadow(Button1 As Button)
    Dim sdk As Phone
    If sdk.SdkVersion < 21 Then Return
    Dim jo As JavaObject
    jo = Button1
    jo.RunMethod("setStateListAnimator", Array(Null))
End Sub

but if i want to use a state list drawable to change button color, i'm not able to change elevation and remove the shadow in first drawable.
How can i do this?
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
Not sure that I understand. I've created a button and changed its colors with the designer. I've added your code and there is no shadow.
This is my code

B4X:
Utils.SetBtnSaveDrawable(BtnSave)
 Dim sdk As Phone
 If sdk.SdkVersion > 20 Then
 Dim jo As JavaObject = BtnSave
 jo.RunMethod("setStateListAnimator", Array(Null))
 End If

Sub SetBtnSaveDrawable(b As Button)
    Dim cdEnabled As GradientDrawable
    cdEnabled.Initialize("TOP_BOTTOM", Array As Int(Azzurro,AzzurroSfumato))
    cdEnabled.CornerRadius = b.Height/2
   
    Dim cdPressed As GradientDrawable
    cdPressed.Initialize("TOP_BOTTOM", Array As Int(Azzurro,AzzurroSfumato))
    cdPressed.CornerRadius = b.Height/2
   
    Dim cdDisabled As GradientDrawable
    cdDisabled.Initialize("TOP_BOTTOM", Array As Int(AzzurroDisabled,AzzurroSfumatoDisabled))
    cdDisabled.CornerRadius = b.Height/2
   
    Dim stdGradient As StateListDrawable
    stdGradient.Initialize
    stdGradient.AddState(stdGradient.State_Pressed, cdPressed)
    stdGradient.AddState(stdGradient.State_Enabled,cdEnabled)
    stdGradient.AddState(stdGradient.State_Disabled, cdDisabled)
    b.Background = stdGradient
End Sub

I saw this shadow. If i use the code to setStateListAnimator i don't see the press effect, but i don't lose shadow in first state
upload_2017-10-12_9-41-10.png
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Check the attached project.
Hi Erel. I tried it and it works. The press effect works, but i really don't understand why i have the shadow in my project...

I tried also to set button.color... shadow is also enabled



Edit: Solved using this code:
B4X:
Dim sdk As Phone
    Dim jo As JavaObject = b
    If sdk.SdkVersion > 20 Then
        Dim f As Float = 0
        jo.RunMethod("setElevation",Array As Object(f))
        jo.RunMethod("setStateListAnimator", Array(Null))
    End If
 
Last edited:
Upvote 0
Top