Search code examples
basic4android

Multiple Button ColorDrawable/State Syntax


Expanding upon my previous question, I am trying to reuse code to assign color/state to multiple buttons. In my last question I forgot the color portion of the code. Here is what I am trying - but getting a Undeclared variable 'buttonprs' is used before it was assigned any value. As I am learning - I'm sure I have syntax issues and your help is appreciated!

Sub SetStateDrawable(Btn As Button, buttonPRS As ColorDrawable, buttonENL As ColorDrawable) 
    buttonENL.Initialize(Colors.Black,0)
    buttonPRS.Initialize(Colors.DarkGray,0)
    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) = buttonSLD.State_Pressed
    buttonSLD.AddState2(buttonSTS, buttonPRS)
End Sub

SetStateDrawable(mybuttonBTN1, buttonPRS, buttonENL)
SetStateDrawable(mybuttonBTN2, buttonPRS, buttonENL)

Solution

  • You have two options: - Remove the two ColorDrawable parameters from the sub signature. - Initialize the parameters before calling this sub.