Android listSelector to TextView widget

Yesterday I spent a lot of time trying to find a way to apply the ListView item highlight effects to a TextView, without any luck.

I first tried it by creating a custom selector in XML, but couldn’t find a way to retrieve the system default colors of different states in order to apply them in my custom selector drawable that I was afterwards assigning to the TextView, by setting it to its backgroundDrawable. But the solution could be “easily” found in the documentation. I navigated to the ListView documentation and I found an XML attribute of the View named listSelector as well as the corresponding setter and getter methods. In the end of the listSelector attribute description there is a mention of the listSelector global attribute resource symbol listSelector. So, here is the answer. Why not use the global listSelector, which is used by default by every ListView widget?

In my TextView configuration, I set the background attribute to the system resource of the list_selector_background:

android:background="@android:drawable/list_selector_background"

Hope this will help people trying to do the same thing!

Good Luck!

Advertisements

About sermojohn

Professional Software Engineer
This entry was posted in Android and tagged , , , , . Bookmark the permalink.

2 Responses to Android listSelector to TextView widget

  1. sochinda says:

    Hi Sermojohn,

    Can I ask you? I want to use button that display Image & text together in android, so pleas tell me what can I do?

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s