Other selected rows on ListView selected but not highlighted - android

If I select ONE row in my ListView, it is highlighted but whenever I enter the MultipleSelection mode (onLongClick, # selected rows, Contextual Action Bar), other rows I select don't remain highlighted BUT is selected according to CAB as it says, "3 selected rows".
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
...
>
<HorizontalScrollView
...
>
<LinearLayout
...
>
<TableLayout
...
>
<TableRow
...
>
<TextView
...
/>
<TextView
...
/>
</TableRow>
</TableLayout>
<ListView
android:id="#+id/db_op_data_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/rowselector"
android:divider="#android:color/transparent" />
</LinearLayout>
</HorizontalScrollView>
</FrameLayout>
rowselector.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/select_button_selectable" />
<!-- focused -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/select_button_selectable" />
<!-- focused and pressed-->
<item android:state_pressed="true"
android:drawable="#drawable/select_button_selected" />
<!-- pressed -->
<item android:drawable="#drawable/select_button_selectable" />
<!-- default -->
</selector>
EDIT: The image looks like this:
Selected Rows but not Highlighted

You need to add state_selected in your selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/select_button_selectable" android:state_focused="true" />
<!-- focused -->
<item android:drawable="#drawable/select_button_selectable" android:state_focused="true" android:state_pressed="true" />
<!-- focused and pressed-->
<item android:drawable="#drawable/select_button_selected" android:state_pressed="true" />
<!-- pressed -->
<item android:drawable="#drawable/select_button_selected" android:state_selected="true" />
<!-- selected (ADD THIS) -->
<item android:drawable="#drawable/select_button_selectable" />
<!-- default -->
</selector>

Related

android:ListSelector doesn't work on my ListView

I want to have an onClick that the chatlist-item changes it's color when clicked/focused. I don't want to use any Java code if possible.
I tried this:
chatlist_layout.xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:background="#drawable/round_corners"
android:listSelector="#drawable/selector">
</ListView>
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="#color/ripple_material_light" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/green_hsrt_1_default_CMYK_100_10_55_0" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/grey" /> <!-- pressed -->
<item android:drawable="#color/black" /> <!-- default -->
</selector>
what am I doing wrong?
Any kind of help will be greatly appreciated
The problem may be in ListView background. So try to add android:drawSelectorOnTop="true"

Why can't set background of checked ListView item?

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>

Change listview row item background color and text color in android

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

ColorStateList doesn't work

I am beginner in android, and of course i have a problem with ColorStateList.
I want to set it to ListView, but it doesn't work, maybe someone know why?
it is declaration of TextView
<TextView
android:textColor="#color/text_color"
android:id="#+id/tv"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/in"
android:onClick="onClick"
android:clickable="true"
android:duplicateParentState='true'/>
and content of text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
It is code from StackOverflow reply, should work :/
Try by setting listview.setItemsCanFocus(true);

Text Color not changing on click

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

Categories

Resources