Dear Fellow Developers
Increasing app engagement is the goal that is in the mind of Every app developer. Why ? because this gives an opportunity to app developer to monetise app.
In my another article Link I demonstrated a way where shape drawables can be used to enhance UI of application and at the same doing this doesn’t increase apk size considerably.
Today we gonna use another shape drawable called StateListDrawable popularly known as selectors . Lets take example of button. A button has few states like pressed, focused , normal etc . By using selectors we can actually provide different drawable resources for different states of button. Have a look at below xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/bg_pressed" /> <item android:drawable="@drawable/bg" /> </selector>
So you can see we have provided different drawable resources for different state. Now you just have to do one thing that you need to set android:background property of your Button element in xml , as this selector file
Now whenever you run your application and press button it will give visual feedbacks to different states of button.
Now for a textview where we want to change the textcolor on some event , we need to modify our selector. Rather than using android:drawable attribute in selector we directly use android:color attribute. So below is our resulting xml.
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="@color/fg_pressed" /> <item android:color="@color/fg" /> </selector>
Now you can use this selector as a value of textColor attribute of your textView.
Thanks for reading.