Android Tutorial StateListDrawable & States Tester

I made a little demonstrative app as a complete example of how StateListManager is used control combinations of states, and how to change either normal background images or 9patch background images based on the state.


You can see the difference between using the same resized image as 9patch drawable or bitmap drawable.
IJrsd.png



You have 3 switches to control the buttons states.
The app shows the current button state both with different images and with text in the middle label.


The app enables at the start the property "FocusableInTouchMode" for both example buttons, so they can be focused (because it's disabled by default on touchscreen devices).

That enables us to notice, for example, that if you press an unfocused button, it gets pressed and then AFTER you release it, it gains the focus.

You can play try new combinations and. And if you improve it, please share it in this thread. Have fun! :)

------
UPDATE 2012-02-23: Added SetTextColorList method, to change the TextColor property depending on the state.
 

Attachments

  • StateListDrawable.zip
    111.3 KB · Views: 879
Last edited:

joseluis

Active Member
Licensed User
Longtime User
I've just updated the program with a method that allows changing the TextColor depending on the state. Enjoy :)

B4X:
Sub SetTextColorList(V As View, DefaultColor As Object, PressedColor As Object, _
   DisabledColor As Object, FocusedColor As Object, FocusedDisabledColor As Object)   
   
   ' Receives 5 states' colors. They can be a Boolean or a String in order to disable that state
   
   Dim sd As StateListDrawable
   Dim StateIndex As Int
   Dim clrs(5) As Int
   Dim states(5,2) As Int
   
   If FocusedDisabledColor Is Int Then
      clrs(StateIndex) = FocusedDisabledColor
      states(StateIndex, 0) = sd.State_Disabled
      states(StateIndex, 1) = 0x0101009c
      StateIndex = StateIndex + 1
   End If
   If DisabledColor Is Int Then
      clrs(StateIndex) = DisabledColor
      states(StateIndex, 0) = sd.State_Disabled
      StateIndex = StateIndex + 1
   End If
   If PressedColor Is Int Then
      clrs(StateIndex) = PressedColor
      states(StateIndex, 0) = sd.State_Pressed
      StateIndex = StateIndex + 1
   End If
   If FocusedColor Is Int Then
      clrs(StateIndex) = FocusedColor
      states(StateIndex, 0) = 0x0101009c
      StateIndex = StateIndex + 1
   End If
   If DefaultColor Is Int Then ' must be the last one
      clrs(StateIndex) = DefaultColor
   End If

   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
 
Top