I would like a Button background to remain a certain color after the button is clicked, and change colors again when some other button is pressed. I thought this was the "state_focused" state.
But the only two states I seem to have for my Button is pressed or not pressed.
Do I understand the state_focused state correctly, or is my StateListDrawable (see below) wrong?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"><shape>
<solid android:color="#00ff00" />
</shape></item>
<item android:state_pressed="true"><shape>
<solid android:color="#ff0000" />
</shape></item>
<item><shape>
<solid android:color="#0000ff" />
</shape></item>
</selector>
state_focused
is whenever the button is focused on using a dpad or the trackball. Views don't generally show a focused state when using touch.
here a example of buttons states:
fonte: http://developer.android.com/design/style/touch-feedback.html
https://developer.android.com/guide/topics/resources/drawable-resource.html
android:state_pressed
Boolean. "true" if this item should be used when the object is pressed (such as when a button is touched/clicked); "false" if this item should be used in the default, non-pressed state.
android:state_focused
Boolean. "true" if this item should be used when the object has input focus (such as when the user selects a text input); "false" if this item should be used in the default, non-focused state.
android:state_hovered
Boolean. "true" if this item should be used when the object is being hovered by a cursor; "false" if this item should be used in the default, non-hovered state. Often, this drawable may be the same drawable used for the "focused" state.
Introduced in API level 14.
android:state_selected
Boolean. "true" if this item should be used when the object is the current user selection when navigating with a directional control (such as when navigating through a list with a d-pad); "false" if this item should be used when the object is not selected.
The selected state is used when focus (android:state_focused) is not sufficient (such as when list view has focus and an item within it is selected with a d-pad).
android:state_checkable
Boolean. "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)
android:state_checked
Boolean. "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked.
android:state_enabled
Boolean. "true" if this item should be used when the object is enabled (capable of receiving touch/click events); "false" if it should be used when the object is disabled.
android:state_activated
Boolean. "true" if this item should be used when the object is activated as the persistent selection (such as to "highlight" the previously selected list item in a persistent navigation view); "false" if it should be used when the object is not activated.
Introduced in API level 11.
android:state_window_focused
Boolean. "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).
I know it's late, from the doco
android:state_focused
State value for StateListDrawable, set when a view has input focus.
May be a boolean value, such as "true" or "false".
From my testing, focus is when the user has navigated to the UI element using a "next/previous" UI like on a soft keyboard, or a remote control device (Android TV), or when the user touches and holds down a button without releasing it. I had to use state_pressed=true and state_focused=true to present a long-pressed UI drawable.
To change a Button's background color and make it persistent after being tapped you simply have to:
- Create a ColorStateSelector XML file
- Set the "backgroundTint" property of the button to the aforementioned XML file
- Set the button's state to the one desired and defined in the ColorState file
As an example in Kotlin and Material Buttons:
ColorStateSelector file (res/color/buttons_color_state.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Red color for pressed state, the pressed state is changed back to normal button color after a certain short time, white in this case-->
<item android:color="#FF1744" android:state_pressed="true" />
<!-- Green color for this state, it's reserved for persistent color change-->
<item android:color="#00E676" android:state_selected="true" />
<!-- White for neutral default state-->
<item android:color="@android:color/white" />
</selector>
Layout XML:
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonOption"
android:backgroundTint="@color/buttons_color_state.xml"
... />
Kotlin file:
val buttonOption = view?.findViewById<MaterialButton>(R.id.buttonOption)
// Implement any "if" checks or other control checks here if necessary
buttonOption.isSelected = true
// now the button is Green!
Best of lucks!
来源:https://stackoverflow.com/questions/8852420/what-is-the-state-focused-state-for-a-button