Issue
I've set a drawable to the tint of my ImageButton so the color of the icon changes automatically when the button is enabled or disabled, is there any way to change the tint programmatically after that so I keep the same behavior just with different colors for the enabled and disabled state?
The content of my_layout.xml:
<ImageButton
android:id="@+id/button_minus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@color/default_button_background"
android:tint="@drawable/button_tint_color"
app:srcCompat="@drawable/ic_remove_24px" />
The content of button_tint_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:color="@color/icon_tint_disable_color" />
<item
android:color="@color/icon_tint_enable_color" />
</selector>
Then in my code I can just do buttonMinus.setEnabled(true)
or buttonMinus.setEnabled(false)
and the icon color change automatically. Is there a way to set programmatically a different color for one or both the enabled color or disabled color ?
Solution
Best way I found so far is to make a new color state list programmatically and assign it to the button, yikes, the goal was to avoid setting visual attributes like colors programmatically ...
ColorStateList buttonStates = new ColorStateList(
new int[][] {
{ -android.R.attr.state_enabled },
{}
},
new int[] {
Color.RED,
Color.BLUE
}
);
buttonMinus.setImageTintList(buttonStates);
Answered By - Bencri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.