Using StateListDrawable to change TextColor with reflection?

joseluis

Active Member
Licensed User
Longtime User
I want to change the TextColor depending on the state of the button (as it is seen here) in the same way I can change the background.

The problem is if I try to asign a StateListDrawable to TextColor property, it fails because it expects an Int:
java.lang.NumberFormatException: (StateListDrawable) android.graphics.drawable.StateListDrawable@40531340

I think it might be doable with the reflection library. But I don't know how. Can anybody help me with this?

Attached is the minimal example.


UPDATE 2014-09-24:
After solving this, I share a complete example where both text and background colors change using statelistdrawable.
 

Attachments

  • StateListDrawableTextColor.zip
    5.6 KB · Views: 190
  • StateListDrawableTextAndBackgroundColor-20140924.zip
    6.4 KB · Views: 174
Last edited:

joseluis

Active Member
Licensed User
Longtime User
Because it would suffer from the same problems I describe here.

The thing is I'm creating a Preferences module (which is progressing very well BTW) and my intention is to mimic the real preferences appearance. In the preferences screen the labels' text color changes depending on the views' state, in the same as the panel's background does. That is one the lasts things remaining in order to mimic its aspect completely, along with the fading of the right side of the title when it doesn't fit the space.

Now I'm guessing the object one must should for asigning colors to states is a ColorStateList. But that object can't be created directly from B4A. That would be ideal, but since it's not, I'll try to create that object and assign it to the property using reflection, although I'm not very confident in my reflection library understanding so far so I'll appreciate some help. ;)
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Well, it doesn't give any error, but the color doesn't change on press either.

EDIT: Problem solved. This is the working source code:
B4X:
Sub Process_Globals
End Sub
Sub Globals
   Dim ButtonExample As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ButtonExample.Initialize("ButtonExample")
   ButtonExample.Typeface = Typeface.DEFAULT_BOLD
   ButtonExample.Text = "TEXT COLOR CHANGES :)"
   Activity.AddView(ButtonExample, 50%x-150dip, 50%y-100dip, 300dip, 200dip)
   SetTextColorList(ButtonExample, Colors.Blue, Colors.Red)
End Sub

Sub SetTextColorList (V As View, DefaultColor As Int, PressedColor As Int)
  Dim sd As StateListDrawable
  Dim clrs(2) As Int
  Dim states(2,1) As Int
   
  clrs(0) = PressedColor
  clrs(1) = DefaultColor
  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 = V
  r.RunMethod4("setTextColor", Array As Object(csl), Array As String("android.content.res.ColorStateList"))
End Sub

Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
 

Attachments

  • StateListDrawableTextColor.zip
    5.7 KB · Views: 195
Last edited:
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Yes! it finally works. It was a problem of priorities!!
The default color must be the last one, because if it's the first one the rest are ignored :)
B4X:
clrs(0) = PressedColor
clrs(1) = DefaultColor
states(0, 0) = sd.State_Pressed
 
Upvote 0

droman

Member
Licensed User
Longtime User
You could give an example of how to also change the background color of the label in this code? I need use another color for background.

Thank you very much.
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Hi droman,

It has been a long time, but I've managed to refresh my memory and improve the example so that now the button's background also changes. The new code is attached to this post.

If you want to see more possibilites you can look at the source code of this example Statelistdrawable States Tester, more complet, and the background changes, both in normal buttons (the left one) as well as 9patch buttons (on the right).

And these other two example are very good also, and a little different: StateListDrawable example and CreateStateListDrawableView with a Bitmap and Pressed Color

---------------------------------------------------

hola droman,

Ha pasado mucho tiempo, pero he conseguido refrescar mi memoria y mejorar el ejemplo para que también cambie el fondo. El nuevo código está adjunto a este mensaje.

Si quieres ver más posibilidades puedes mirar el código de este otro ejemplo Statelistdrawable States Tester, mucho más completo, y donde cambia el fondo del botón, tanto para botones normales (el de la izquierda) como usando 9patch (derecha).

Y estos otros dos ejemplos también están muy bien y algo distintos: StateListDrawable example y CreateStateListDrawableView with a Bitmap and Pressed Color
 

Attachments

  • StateListDrawableTextAndBackgroundColor.zip
    6.4 KB · Views: 190
Upvote 0

droman

Member
Licensed User
Longtime User
Thanks JoseLuis!

I'm very busy , but I 'll try the code you have posted.

-------------------------------------------

¡Gracias JoseLuis!

Voy a probar el código que has publicado, a la primera no me cambia el fondo, pero creo que es por que no tengo demasiado tiempo para programar y no estoy al 100%.

Muchas gracias.
 
Upvote 0
Top