Android ListView Selector Color

Hi All,

I have 2 questions regarding a ListView in Android:

  1. How can I get the color of the listview's focused row ? I tried to use the ListView.getSelector() method, which according to its documentation should give me what I'm looking for, but it's giving me a Drawable object which I don't know how to retrieve the color from (if possible...).

  2. How can I set the color of the listview's focused row ? Here I tried to use the setSelector() method on the listview, passing it a ColorDrawable object, but the result of doing it is that the whole background of the list view is painted in that color... and this is not what I wanted of course...

Thanks!


Asked by: Abigail527 | Posted: 20-01-2022






Answer 1

The list selector drawable is a StateListDrawable — it contains reference to multiple drawables for each state the list can be, like selected, focused, pressed, disabled...

While you can retrieve the drawable using getSelector(), I don't believe you can retrieve a specific Drawable from a StateListDrawable, nor does it seem possible to programmatically retrieve the colour directly from a ColorDrawable anyway.

As for setting the colour, you need a StateListDrawable as described above. You can set this on your list using the android:listSelector attribute, defining the drawable in XML like this:

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

Answered by: Oliver231 | Posted: 21-02-2022



Answer 2

TO ADD: @Christopher's answer does not work on API 7/8 (as per @Jonny's correct comment) IF you are using colours, instead of drawables. (In my testing, using drawables as per Christopher works fine)

Here is the FIX for 2.3 and below when using colours:

As per @Charles Harley, there is a bug in 2.3 and below where filling the list item with a colour causes the colour to flow out over the whole list. His fix is to define a shape drawable containing the colour you want, and to use that instead of the colour.

I suggest looking at this link if you want to just use a colour as selector, and are targeting Android 2 (or at least allow for Android 2).

Answered by: Daniel613 | Posted: 21-02-2022



Similar questions

listview - Android Selector Problem

i have a listview with a custom item_row.xml. I've defined a selector in this way: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;item android:state_pressed="false" android:drawable="@drawable/list_bg" &gt; &lt;/item&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/header_bg" &gt; &lt;/item&gt; &lt;item androi...


android - ListView rounded Selector

I have create a listView with rounded cornered background. Now while i select the first item in the listview, the selector shape was not top rounded rect. Likewise while i select the last item in listview, the selector shape was not bottom rounded rect. If i apply rounded rect to the selector all listview item selection will be rounded rect shape. So that i want the selector shape to be matched with the listview's rounded ...


listview - Android Selector Problem

i have a listview with a custom item_row.xml. I've defined a selector in this way: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;item android:state_pressed="false" android:drawable="@drawable/list_bg" &gt; &lt;/item&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/header_bg" &gt; &lt;/item&gt; &lt;item androi...


android - ListView rounded Selector

I have create a listView with rounded cornered background. Now while i select the first item in the listview, the selector shape was not top rounded rect. Likewise while i select the last item in listview, the selector shape was not bottom rounded rect. If i apply rounded rect to the selector all listview item selection will be rounded rect shape. So that i want the selector shape to be matched with the listview's rounded ...


Android - ListView list selector doesn't use default

I'm using my own selector for my ListView, but the default is not used. How are you supposed to set the default background drawable? With the below code I'd expect all of my list rows to be blue until focused or pressed. &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" &gt; &lt;shap...


Android: ListView Selector set to null?

I want to disable the Selector from my ListView. That's when I click any items there will be no indication that it has been clicked. I tried this in my ListView tag: android:listSelector="@null" and didn't work


Android ListView Item Selector appear for the whole list

i want to change the default selection color of my listview items in my list as followed: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_focused="true" android:state_pressed="false" android:drawable="@color/Blue" /&gt; &lt;item android:state_focused="false" android:state_pressed...


Android ListView selector not working well

`I am utilizing list selector, but its not working acoordingly. I am gettings effects after clicking list item, but there is no effect before item is clicked. Here is my code for selector: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_selected="false" android:state_pressed="false" andro...


listview - android list item selector and divider

I have a ListView with a custom divider. Divider is just some color and is 3dp in height. I've added a custom selector to each item in the list, so the background color would be changed when item is clicked. My selector looks like this: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;: &lt;item android:state_selected="true" android:...


Android ListView selector

I have a ListView in my application with following properties: android:layout_width="match_parent" android:layout_height="wrap_content" android:choiceMode="singleChoice" android:clickable="true" android:fastScrollEnabled="true" android:drawSelectorOnTop="true" android:focusable="true" android:focusableInTouchMode="true" But I can not see the selector on scr...


android - Why is the first item in my listview using a different selector on tablets?

This happens on a Google Nexus 10 and it's working properly on phones So I need some reputation before I can post any images, however you can check it out here: http://i.imgur.com/2tG6b0h.png The listview is working perfectly except when it comes to the first item. It gets drawn with different coloring (namely I'm noticing transparency being...


android - ListView Item shadow + custom selector

I want to drop shadow at ListView item and also apply custom selector. But i don't know how to apply them at the same time.. Here is shadow.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item &gt; &lt;shape android:shape="rectangle"&gt; &lt;solid android:color="@android:color/darker_gray" ...


android - How to pass more than one array to a listview?

I am using a ListView to display items. Currently I am passing a String array of items to it. But I want to pass one more array and display its items along with the items of the first array; that is, somehow having two lines of text. How can I do that?


Using a ListView to create a settings screen in Android?

I'm developing my first Android application, and I'd like to create a settings screen. I'd like the screen to have a similar look-and-feel as the native phone-settings screens and the native "create/edit alarm" screen. Thus with different kinds of (statically defined) items vertically stacked with a thin line between them. How do I define such screen? I understand I can use the ListView, but this ...


android - Which is better loading of images for ListView?

I am wondering if which of the two is better in loading images in a listview from web, is it by batch through some number of threads that are running simultaneously or one by one through thread queue? I have noticed (but I don't know if that is really the implementation) from the youtube app that the images are loaded by batch and it is kinda fast. Even for not only loading images but also requesting some data from...


how to list the audio files in a Listview in android

I am new to this android application development. I have a audio files in a particular directory. I want to list the those audio files in a listView. please tell me how to do this activity.


android - Making child ListView scroll

Hello I am trying to make a terminal type app that is wider than the actual android screen. A listview is great except that I don't want the text to wrap. If the text is wider than the screen I want it to print the full text and then the user can scroll over horizontally if they want to see what was printed off the screen. Just FYI this is for ascii type tables. I have followed this example:


listview - How to popup list like a spinner without spinner in android?

I have a spinner widget in my activity which lets users pick a list name. Normally, the function of the spinner is to switch between lists but for a couple of instances, I swap out the selection change listener to perform a different function with the same list of options. Once the selection has been made, the old listener is restored and life goes on. This is a bad and buggy arrangement. Instead, I would...


android - display item of listview

I have a list view having several items and it is multichoice list. I want to display the checked item of list view. how i can do this.can anyone help me???


android - Specify view to use when ListView is empty?

I can't find this in the docs - isn't there a way to specify a View to use for a ListView if the adapter is empty? Thanks


listview - Round bottom corners on Android Bitmap?

Hey all, what would be best practice for clipping the bottom borders of a Bitmap? Just manipulate the Bitmap itself or overlay an alpha mask drawable or ...? The whole story: I've a Listview which looks like the iPhone's grouped UITableView style. I would like to display a Bitmap in the last row, but for now the Bitmap overlaps my custom background drawable of the Listview cell. Thx in adva...


java - How do I fill a ListView (in Android) with XML or JSON data?

I read a tutorial, and it uses SQLlite and "SimpleCursorAdapter" to fill the list with items. This is the code the tutorial taught me. private void fillData() { // Get all of the notes from the database and create the item list Cursor c = mDbHelper.fetchAllNotes(); startManagingCursor(c); String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; int[] to = new int[]...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top