UI part of every application is the key for user engagement, this fact is known by all.
Building a better UI in android is making better and interactive views (xmls).
While working i just wanted to change the color of a button when the button is diabled from default color to some color of my choice (like just change the alpha value or some light toned similar color, so that the user understands that the button is disabled).
Lets have a look at how to implement the same :
- Create a new drawable file with selector as the parent tag
- Apply this drawable file on your button’s background
CODE : my_button_background_selector
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Color when the row is selected --> <item android:state_enabled="true" android:drawable="@color/my_button_enabled_state_color" /> <!-- Standard background color --> <item android:drawable="@color/my_button_disabled_state_color" /> </selector> |
Now you have to add only one line to your button in your xml file.
1 2 3 4 5 6 7 |
<Button android:id="@+id/my_btn" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/my_button_background_selector" android:text="my button" /> |
And it is done.
You can also replace the color with your custom drawable file.
Keep coding , keep sharing 🙂