Android Code Snippet ColorStateList

Similar threads

B4A Tutorial Cards list with CustomListView
B4A Code Snippet Change CheckBox colors
B4A Code Snippet Switch Colors
B4A Code Snippet Change EditText Colors
B4A Tutorial StateListDrawable example

stevel05

Expert
Licensed User
SubName: SetColorStateList

Description:

Along with setting the background to buttons using a StateListDrawable, we can also change the color of the text (as with the default buttons) in a similar manner using a ColorStateList object.

This sub will set the Pressed, Enabled and Disabled text colors for a button. You can add more states if required, get the constant values from the StateListDrawable constants or from the documentation : http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

You must provide as many colors as states.

Depends on: JavaObject

B4X:
'Pass the Button object and Colors int values to set for each state.
Sub SetColorStateList(Btn As Button,Pressed As Int,Enabled As Int,Disabled As Int)

    Dim States(3,1) As Int
    States(0,0) = 16842919    'Pressed
    States(1,0) = 16842910    'Enabled
    States(2,0) = -16842910 'Disabled

    Dim Color(3) As Int = Array As Int(Pressed,Enabled,Disabled)

    Dim CSL As JavaObject
    CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,Color))
    Dim B1 As JavaObject = Btn
    B1.RunMethod("setTextColor",Array As Object(CSL))

End Sub
Tags: ColorStateList Button
 
Last edited:

LucaMs

Expert
Licensed User
Thank you for your snippet, Steve.

I think it is possible to achieve the same result without using JavaObject.
B4X:
Private Sub SetColorStateList(Btn As Button, Pressed As Int, Enabled As Int, Disabled As Int, CornerRadius As Int)
    Dim cdPressed, cdEnabled, cdDisabled As ColorDrawable
    cdPressed.Initialize(Pressed, CornerRadius)
    cdEnabled.Initialize(Enabled, CornerRadius)
    cdDisabled.Initialize(Disabled, CornerRadius)
  
    Dim sld As StateListDrawable
    sld.Initialize
    sld.AddState(sld.State_Pressed, cdPressed)
    sld.AddState(sld.State_Enabled, cdEnabled)
    sld.AddState(sld.State_Disabled, cdDisabled)

    Btn.Background = sld
End Sub
 

stevel05

Expert
Licensed User
Hi Luca,

Your code sets the Button Image, the code snippet set's the Button text color.

Steve
 

LucaMs

Expert
Licensed User
B1.RunMethod("setTextColor",ArrayAsObject(CSL)) :)

You should change the name to TextColorStateList, so next time I will not write nonsense :D

Well, I'll post it as a new snippet
 

tremara1

Active Member
Licensed User
Just a quick query, when I tried this snippet I got an error on load .....
Error description: '(' expected.
Occurred on line: 142
CSL.InitializeNewInstance("android.content.res.ColorStateList",Array As Object(States,color))
Word: )
It seems to want parameter after color "array as Object(States,color)"

Thanks....
 

stevel05

Expert
Licensed User
How are you calling the sub, can you post the code?
 

tremara1

Active Member
Licensed User
B4X:
SetColorStateList(tempBut,Colors.DarkGray,Colors.gray,Colors.gray)
Is the call
and the sub is as posted from above......
 
Top