Android ListView displays ListSelector only after scrolling - android

I added a simple ListSelector to the ListView.
The Problem is, that the ListSelector is only displayed, after the ListView is scrolled a little.
How can I make the ListSelector to apear immediately on select?
ListView
<ListView
android:id="#+id/contentListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="#id/buttonEntwerten"
android:scrollingCache="false"
android:cacheColorHint="#00000000"
android:listSelector="#drawable/list_selector"
/>
#drawable/list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector__gradient_bg_hover" />
</selector>

you can check out how android has configured it, and change it accordingly (file name is "list_selector_background.xml") :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focus" />
</selector>
in short, you just have to handle the state_pressed state . the rest are good states to handle too.
the problem in your code is that it has no states.

Related

ListView multi select - transparent color by default does not work correctly?

I have multi select on my list view. Selector looks like.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/wt_list_click" />
<item android:state_selected="true" android:drawable="#color/wt_list_click" />
<item android:state_activated="true" android:drawable="#color/wt_list_click" />
<item android:drawable="#android:color/transparent" />
</selector>
But it does not work correctly.
Some strange behavior. At the end of the selection is always one cell.
If I change color from transparent to any other, everything works correctly.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/wt_list_click" />
<item android:state_selected="true" android:drawable="#color/wt_list_click" />
<item android:state_activated="true" android:drawable="#color/wt_list_click" />
<item android:drawable="#color/red" />
</selector>
What is the problem, why it is so. Can someone explain to me.
In the picture, I was initially selected 5 cells, then unselected one cell. But 5 cells remained selected.
I solved!!! I founded on stackowerflow.
<ListView android:listSelector="#android:color/transparent" android:cacheColorHint="#android:color/transparent" />
and my selector on root layout from list_item
android:background="#drawable/list_item_click_selector"
https://stackoverflow.com/a/12242564/1590594

How to Customize ListView Seletion in Android

I want to customize the selection effect of the items in the listView, but I don't know the way.
Will be useful if I should change the selection item background with a different png when the user select the item.
Any suggestion?
Use a selector for this:
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:divider="#null"
android:dividerHeight="0dip"
android:listSelector="#drawable/list_selector" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/yourdrawable" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/yourdrawable" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/yourdrawable" />
<item android:drawable="#drawable/yourdrawable" />
</selector>
Here is a good tutorial for it.
What you are looking for is selector
Create a selector xml in res and set the background for the list item to the selector xml.
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/selected" />
<item
android:drawable="#drawable/normal" />
</selector>
Also look at:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

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:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/ic_launcher" />
<item android:state_pressed="true"
android:drawable="#drawable/headerplain" />
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
</selector>
and I have applied it to listView as
<ListView
android:id="#+id/listView123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
android:focusable="true"
>
</ListView>
Please help me to get out of this problem
Thanks in advance
This is a good tutorial explaining very well how selector works
I have rowback and rowback_selected in my drawable folder...
and Use android:state_focused,android:state_selected,android:state_pressed...
<item android:state_focused="true" android:drawable="#drawable/rowback" />
<item android:state_pressed="true" android:drawable="#drawable/rowback_selected" />
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#drawable/rowback_selected" />
<item android:state_window_focused="false"
android:drawable="#drawable/rowback" />

Highlight mysterious behaviour in ListView

I have set a custom list selector for a Simple ListView but when I select an item, the whole ListView turns blue. I don't understand where the problem is.
This is my ListView:
<ListView android:id="#+id/TopListView" android:layout_width="fill_parent"
android:listSelector="#drawable/regular_selector"
android:layout_height="wrap_content">
</ListView>
and it's the regular_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/blue" />
<item android:state_focused="true"
android:drawable="#color/blue" />
</selector>
I was having the same issue of the whole list view highlighting, when using a color as the background. Curiously this only happened below api 11.
Solution was to use a solid shape drawable to wrap the colour:
list_selector_shaped_background_press.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/list_selector_pressed"/>
</shape>
List_selector_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
I guess it is the usual problem. You have to set this in your listview:
android:cacheColorHint="#00000000"
The goal is to disable an optimization that the framework does for improving drawing performance during scrolling operations.
When I set the regular_selector.xml as listview item background it work!

Android: Entire ListView changes color on focus, not just ListView child item

I am trying to apply a selector to a ListView to make it easy for those without touch screens to navigate my app. The thing is, by applying the selector to the ListView, it only seems to apply the background colors to the entire list, not the items inside of it.
Any ideas? Here's some code:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#drawable/listselector"
/>
<TextView android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are no Clients yet."
/>
listselector.xml in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector
android:id="#+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/darkblue" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/green" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/green" />
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/green" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="#color/green" />
</selector>
The reason you're seeing the focused colour being applied to the whole list is because you have directly referenced a colour value for the focused and pressed states. There is a known bug in Android v2.3 and below where the colour drawable doesn't honour its bounds in these circumstances.
To fix this issue you can create a shape drawable using the desired colours and reference that instead.
e.g define the shape in 'drawables/list_selector_focused.xml' as:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/green" />
</shape>
And then reference that in the selector:
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/list_selector_focused" />

Categories

Resources