Android Question StateListDrawable and Button

Anser

Well-Known Member
Licensed User
Longtime User
Friends,

I have a button. There will be 3 states for the button.

1. Enabled
2. Disabled
3. Pressed

When I load the activity, initially the button will be in disabled state and from the Sub Job_Done the button will get either enabled or disabled based on a condition.

I have used the following code, but I can't get it working. When I press the button, there is no difference in the Color of the Button in all the above said states. Can anyone one tell me what am I doing wrong ?

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("MyLayoutFile")

    ' I need the button to be round
    Dim nRadius As Int
    nRadius = DipToCurrent((BtnSave.Height + BtnSave.Width)/2)

    Dim EnabledClr,PressedClr,DisabledClr As ColorDrawable
    EnabledClr.Initialize(Colors.Blue, nRadius)
    DisabledClr.Initialize(Colors.Red, nRadius)
    PressedClr.Initialize(Colors.Green, nRadius)

    'Create StateListDrawable
    Dim sld As StateListDrawable
    sld.Initialize
    sld.AddState(sld.State_Enabled,EnabledClr)
    sld.AddState(sld.State_Disabled,DisabledClr)
    sld.AddState(sld.State_Pressed,PressedClr)

    BtnSave.Background = sld

    'Here I expect the button to be displayed in the Disabled Color
    BtnSave.Enabled = False

End Sub

Sub BtnSave_Click
   Dim nResponse As Int
   nResponse = Msgbox2("Shall I save this data ?","Confirmation required","Yes","","No",Null)
   If nResponse = DialogResponse.POSITIVE Then
     Msgbox("Saved","OK")
   Else
     Msgbox("Not Saved","NOT OK")
   End If
End Sub

Initially I tried with the AppCompat ACFlatButton. As I could not get it working, I changed it to regular Button.

Any help will be appreciated.

Regards
Anser
 
Top