Search code examples
basic4android

Reducing button statelistdrawablecode


I have this code working fine a few times in my project but was wondering the easiest way to allow it to be used for multiple buttons without repeating the code. For example - I have mybuttonBTN1 below and would like to add mybuttonBTN2, etc. - I've tried a few ways but always get weird results.

Dim buttonSLD As StateListDrawable
buttonSLD.Initialize
Dim buttonSTS(2) As Int
buttonSTS(0) = buttonSLD.State_Enabled
buttonSTS(1) = -buttonSLD.State_Pressed
buttonSLD.AddState2(buttonSTS, buttonENL)
Dim buttonSTS(1) As Int
buttonSTS(0) = bbuttonSLD.State_Pressed
buttonSLD.AddState2(bbuttonSTS, buttonPRS)
mybuttonBTN1.Background = buttonSLD

Solution

  • Something like:

    Sub SetStateDrawable (Btn As Button, Pressed As Drawable, Enabled As Drawable)
     Dim buttonSLD As StateListDrawable
     buttonSLD.Initialize
     Dim buttonSTS(2) As Int
     buttonSTS(0) = buttonSLD.State_Enabled
     buttonSTS(1) = -buttonSLD.State_Pressed
     buttonSLD.AddState2(buttonSTS, Pressed)
     Dim buttonSTS(1) As Int
     buttonSTS(0) = bbuttonSLD.State_Pressed
     buttonSLD.AddState2(bbuttonSTS, Enabled)
     Btn.Background = buttonSLD
    End Sub
    
     SetStateDrawable(myButtonBTN1, buttonPRS, buttonENL)