I'm using SherlockListFragment to show list items, and I want to set the background of list item highlighted when it is checked. But after I did all the things below, the list item background still remain unchanged when set checked (but it changes when clicked). Why?
The list selector => drawable/list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="#color/transparent" />
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="#drawable/list_selector_background_dark" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_dark" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_dark" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_dark" />
<item android:state_checked="true"
android:drawable="#drawable/list_selector_background_dark" />
</selector>
The list item layout => layout/list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:background="#drawable/list_selector_dark">
......
</RelativeLayout>
The fragment layout => layout/fragment_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="start"
android:scrollbars="none"
android:fastScrollEnabled="true"
android:choiceMode="singleChoice"
android:listSelector="#drawable/list_selector_dark" />
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/no_entry" />
</LinearLayout>
Related
In my custom listview, I have used relativelayout and inside that added 2 textviews vertically.
Background image is set to relativelayout. Now I want to change background image as well as textcolor of textview when listview item is pressed.
How to do this, any one have idea?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/item_bg"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/orange"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="20dip"
android:layout_marginTop="8dip"
android:ellipsize="end"
android:maxLines="3"
android:duplicateParentState="true" <!-- this is a line to use..-->
android:textColor="#color/_textcolor"
android:textSize="14sp" >
</TextView>
item_bg.xml inside res/drawable folder:
<?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/bannerbg_hover"/>
<item android:state_focused="true" android:drawable="#drawable/bannerbg_hover" />
<item android:state_selected="true" android:drawable="#drawable/bannerbg_hover"/>
<item android:drawable="#drawable/bannerbg"/>
</selector>
_textcolor.xml inside res/color/ folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#color/white"/>
<item android:state_focused="true" android:color="#color/white" />
<item android:state_selected="true" android:color="#color/white"/>
<item android:color="#color/dark_blue"/>
</selector>
Concerning the background, you should create a StateList Drawable and set it as the background of your rows.
Ex of a such drawable (could be named background_selector.xml) :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/color1" /> <!-- pressed -->
<item android:drawable="#color/color2" /> <!-- default -->
</selector>
And in the layout declaration of your row :
...
android:background="#drawable/background_selector"
...
Use the same way for your text with the Color State List
I have a List View and i am setting the background of the list item with the selector.
list_selecter.xml :
<?xml version="1.0" encoding="utf-8"?>
<!-- focused and pressed -->
<item android:drawable="#color/darkred" android:state_pressed="true"/>
<item android:drawable="#android:color/transparent" android:state_pressed="false"/>
<!-- pressed -->
<item android:drawable="#color/darkred"/>
Layout for the List Item is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selecter"
android:orientation="vertical"
android:padding="10dp" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsHeadingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textColor="#color/text_selector" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="#drawable/aerow" />
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsText"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_below="#id/newsHeadingText"
android:textColor="#736F6E" />
</RelativeLayout>
And the text_selector.xml for the text color change:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/white"/>
<item android:state_focused="true" android:color="#android:color/white"/>
<item android:state_pressed="true" android:color="#android:color/white"/>
<item android:color="#android:color/black"/>
</selector>
Text color is not getting changed on the selection. What could be the issue?
Specify android:duplicateParentState='true' on your TextView
I used this SO question as a guide, but when I click a row it doesn't stay highlighted. What is wrong with my code?
score_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/score_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/keyboard"
android:listSelector="#drawable/selector"
android:choiceMode="singleChoice"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
...
</RelativeLayout>
drawable/selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/highlight"/>
<item android:state_pressed="true" android:drawable="#color/highlight"/>
</selector>
score_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scoreRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#drawable/row_selector" >
...
</LinearLayout>
drawable/row_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#color/highlight" />
<item android:state_activated="true" android:drawable="#color/highlight" />
<item android:drawable="#color/transparent" />
</selector>
This code now works properly.
Try to use state_activated for your row_selector.
I have a custom dialog which extends the Dialog class and a simple custom layout like the one from below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:drawable/list_selector_background"
>
<TextView
android:id="#+id/text_view"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:background="#color/transparent"
android:textColor="#color/black"
android:textSize="16sp"
android:focusable="true"/>
My problem is that the trackball makes the text not visible when a row is selected like in the attached image.
The list_selector_background code is from Android and it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<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" />
And my ListView xml code is this:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
android:background="#color/white"
android:cacheColorHint="#color/transparent">
</ListView>
I saw that on an AlertDialog (the one from Android) is working okay. What should I do to solve this problem?
try this: remove "android" text from this line. like;
android:background="#drawable/list_selector_background"
add in list view
android:cacheColorHint="#android:color/transparent"
android:drawSelectorOnTop="false"
android:listSelector="#android:color/transparent"
android:fadingEdge="none"
this one work for me
I am using this tutorials and I want to add selectors for this list view. I tried some codes but it doest work. How can I do it.
I used code as list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
<item android:state_pressed="true"
android:drawable="#drawable/list_selector_background_pressed" />
</selector>
and my List view
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
Just change a word here from listSelector to background. Because you want it as the background drawable selector, right?
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
EDIT:
try changing your selector file to (I changed the order of the two items):
<?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/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>