TextView drawables & XML state list resource

Its painfully bad to change graphic of a TextView drawable programmatically. If you want android:drawableRight, android:drawableLeftandroid:drawableTop or android:drawableBottom to change based on the TextView state it is possible to do it simply the same way you would specify to a android:background attribute. There should be no need to change any of these properties programmatically. It is much more neat & efficient to deal with this in the layout file.

e.g. in your layout file:


icon_accept_selector is an XML state list drawable file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_accept" android:state_enabled="true"/>
    <item android:drawable="@drawable/icon_accept_disabled" android:state_enabled="false"/>

One thought on “TextView drawables & XML state list resource

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s