StateListDrawable is a drawable objects that holds other drawables. Based on the view's current state a child drawable is selected.
StateListDrawable holds a list of states. States can be "disabled", "enabled", "checked" and so on.
A state can also be a combination of other states (like enabled and pressed).
The StateListDrawable holds a list of states and drawables pairs. Whenever the view's state changes the list is scanned and the first item that matches the current state is chosen.
Matching means that all elements in the state item are included in the current state.
This means that an empty state item will match all current states.
The order of states added is very important as the first item matches will be chosen.
This example creates a ToggleButton and sets its two states (checked and unchecked) to two ColorDrawables.
Note that in this case the order is not important as no state is contained in another.
StateListDrawable holds a list of states. States can be "disabled", "enabled", "checked" and so on.
A state can also be a combination of other states (like enabled and pressed).
The StateListDrawable holds a list of states and drawables pairs. Whenever the view's state changes the list is scanned and the first item that matches the current state is chosen.
Matching means that all elements in the state item are included in the current state.
This means that an empty state item will match all current states.
The order of states added is very important as the first item matches will be chosen.
This example creates a ToggleButton and sets its two states (checked and unchecked) to two ColorDrawables.
B4X:
Sub Process_Globals
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
Dim tb As ToggleButton
tb.Initialize("") 'no events will be caught
Dim checked, unchecked As ColorDrawable
checked.Initialize(Colors.Green, 10dip)
unchecked.Initialize(Colors.Red, 10dip)
Dim sld As StateListDrawable
sld.Initialize
sld.AddState(sld.State_Checked, checked)
sld.AddState(sld.State_Unchecked, unchecked)
tb.Background = sld
tb.Checked = True
tb.TextColor = Colors.Blue
tb.TextSize = 20
tb.Typeface = Typeface.DEFAULT_BOLD
Activity.AddView(tb, 100dip, 100dip, 100dip, 100dip)
End Sub