Changing color of selected text on label?

ondesic

Active Member
Licensed User
Longtime User
I noticed that when I press a lebal, the text automatically changes colors to black. I love this. It is a subtle way to know you pressed it.

My problem is, if I need the label to start with black text, this cool effect doesn't happen. Is there a way to make the text turn to a different color other tan black when you press the label?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It can be done with the Reflection library:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim l As Label
   l.Initialize("l")
   Activity.AddView(l, 0, 0, 100dip, 100dip)
   l.Text = "Some text"
   SetColorList(l, Colors.Yellow, Colors.Blue)
End Sub

Sub l_Click

End Sub

Sub SetColorList (Label1 As Label, DefaultColor As Int, PressedColor As Int)
   Dim sd As StateListDrawable
   Dim clrs(2) As Int
   Dim states(2,1) As Int
   clrs(0) = DefaultColor
   clrs(1) = PressedColor
   states(0, 0) = sd.State_Pressed
   Dim r As Reflector
   Dim csl As Object
   csl = r.CreateObject2("android.content.res.ColorStateList", Array As Object(states, clrs), _
      Array As String("[[I", "[I"))
   r.Target = Label1
   r.RunMethod4("setTextColor", Array As Object(csl), Array As String("android.content.res.ColorStateList"))
End Sub
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
Is this even possible for the background color of a label?
I tried setColor, setBackgroundColor instead of setTextColor but I'll always get an error...


specci48
 
Upvote 0
Top