dont work select single item and highlight listview android - android

I'm selecting
lst_Center.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and adding a listView:
android:choiceMode="singleChoice"
and LinearLayout:
android:background="#drawable/list_selector"
And adding a list_selector.xml:
<item android:drawable="#color/Blue" android:state_enabled="true"/>
<item android:drawable="#color/Red" android:state_pressed="true"/>
But it's not working, how can I fix it?

Create list_background.xml in drawable folder and add below lines.
<?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/anycolor" />
<item android:state_focused="true" android:drawable="#color/anycolor" />
<item android:drawable="#color/anycolor" />
</selector>
Set background for your ListView.
android:background="#drawable/list_background"
If you want to highlight only the selecting list item means then you should set the background for textview. For that just go through about custom adapter.
Hope it helps you.

Related

How to make listview selected item selectable?

I am implementing an endless ListView (like in the Facebook app). I want to make item as select(temporarily show background as grey) which I select. when I click on listview, nothing happens(not shown as grey). I tried setting android:focusable="false" and android:cickable="false" for my List view contents. But I didn't notice any chanage What is my fault? Any One help me. Thanks in advance.
You can accomplish this by using a selector which you can set as the background of your list item.
drawable/background_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gray" android:state_pressed="true" />
<item android:drawable="#color/gray" android:state_selected="true" />
<item android:drawable="#color/gray" android:state_activated="true" />
<item android:drawable="#color/default_color" />
</selector>
layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_list_item"
android:orientation="vertical">
<... />
</LinearLayout>
If you want to show the clicked option as highlighted. Don't put clicklistener to convetview. Use
ListView.SetOnclicklistener(..){
...
}
That is best one to remove the issue.
Try this as background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/someGreyColor" android:state_activated="true"/>
<item android:drawable="#color/anyColor" android:state_pressed="true"/>
<item android:drawable="#color/backgrounColor"/>
</selector>

How to set the initial ListView item background color with a selector

I cannot wrap my head around this, I want to set the initial background color of my list view items to grey, but they only take the right background color after I have selected them (and deselected) the first time. Which attributes are not configured properly in the first item definition?
<?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="#color/grey" />
<item android:state_pressed="true" android:drawable="#color/red"/>
</selector>
Also tried to set the background of the list view item directly, but then the selector does not have any effect anymore.
For list items define a selector drawable:
res/drawable/list_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#color/red" /> <!-- Pressed state -->
<item android:state_enabled="true" android:state_focused="true" android:drawable="#color/black" /> <!-- Focused state -->
<item android:state_enabled="true" android:state_selected="true" android:drawable="#color/orange" /> <!-- Selected state -->
<item android:drawable="#color/gray" /> <!-- Default state -->
</selector>
Set the above drawable as background for individual list item:
res/layout/list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="#drawable/list_item_background" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listitem" />
</LinearLayout>
Use the above custom layout for creating the list items:
String[] mStrings = {"One", "Two", "Three", "Four"};
ListView list = (ListView) rootView.findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, R.id.listitem ,mStrings));
I would suggest trying with the following definition:
<?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/red"/>
<item android:drawable="#color/grey" />
</selector>
State List Drawables try matching in order. If you want a "default" drawable for many cases, it should be the last one, with no state flags. From docs:
During each state change, the state list is traversed top to bottom
and the first item that matches the current state is used—the
selection is not based on the "best match," but simply the first item
that meets the minimum criteria of the state.
Also tried to set the background of the list view item directly, but then the selector does not have any effect anymore.
If I am not mistaken, selector is drawn behind the list item(by default). Since you set solid grey color to your list item, the red color from the selector doesn't show. You can confirm this by changing the way you have defined #color/grey - try adding a low alpha value to it. In this case the red color should come through.
You can of course change this behavior by setting android:drawSelectorOnTop="true" - default is false. The list item will however be hidden when the selector is drawn on top.
Anyway - from what you have said, you want the items to have #color/red background when pressed and #color/grey background otherwise.
See if the following satisfies your requirements:
Selector drawable for list item views:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/transparent"/>
<item android:drawable="color/grey" />
</selector>
For the listSelector attribute, only provide the color value:
<ListView
....
....
android:listSelector="#color/red" >
</ListView>
In a few words, the item background color is transparent when the list item is pressed - allowing the listSelector red color to be visible. In all other cases, the grey color is visible.
You need to create a listselector.xml under your drawable folder
<?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/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
Then inside your listview add :
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/listselector" />
The selector should be similar to the one below:
List_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="#drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/gradient_bg_hover" />
</selector>
Then in the drawables folder, you need to create the following 2 files:
gradient_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#cfcfcf"
android:centerColor="#737374"
android:endColor="##3e3e3e"
android:angle="270" />
</shape>
gradient_bg_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#7f0000"
android:centerColor="#660000"
android:endColor="#4c0000"
android:angle="270" />
</shape>
Finally, in the listview, you need to add the following line of code:
android:background="#drawable/list_selector"
Hope this helps :)

Setting a custom indicator for ExpandableListView isn't working

So I'm trying to set a custom ExpandableListView indicator icon but it isn't working. I created an icon and saved it to drawable folder. Here are the icons:
This is the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/arrow_up" android:state_empty="true"/>
<item android:drawable="#drawable/arrow_down" android:state_activated="true"/>
</selector>
ExpandableListView xml:
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/custom_arrow" />
However, when I launch the app, there is not indicator at all. Any idea what I have done wrong?
this should do the trick
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="#drawable/arrow_up" />
<item android:drawable="#drawable/arrow_down" />
</selector>
you wanted the state_expanded for when the row gets expanded and then any other state besides expanded is just the normal down arrow. You should however handle the state_empty when there is nothing to expand to
I think you just need to add this one line to your selector and change the arrow down condition:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/arrow_up" android:state_empty="true"/>
<item android:drawable="#drawable/arrow_down" android:state_expanded="true"/>
<item android:drawable="#drawable/arrow_up" />
</selector>

How to change the textview color of item listview when that item is on click?

I have a ListView and each item have a TextView.
I would like change the item TextView color, for example, when user click in that item.
any ideas, examples or tutorials?
thanks
use a list of states.
For example, create a file called bg_states.xml in your drawable folder an:
<?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/blue" ></item>
<item
android:state_pressed="true"
android:drawable="#color/blue" ></item>
<item
android:drawable="#color/black" />
</selector>
and define it as a background for your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_states"
try it with
v.setBackgroundColor(R.Color.Red);
in your onClick method

Can't use default ListView selector

I'm developing a ListView app for Android. I want to use the default ListView selector, for example the one in Market when you touch an item.
I don't get a highlight in my ListView, where's the way to correctly implement it? I've read about the "Touch mode" although I can't get it to work.
Thanks in advance!
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
You have to assign to each row layout a background composed by a selector that assign a different background for each state. Suppose that your item row layout is a RelativeLayout defined in a xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:background="#drawable/transparent_selector"> ...
and transparent_selector is defined here: watch that carefully one drawable shape belongs to a single state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/shape_7"/>
<item android:state_pressed="true" android:drawable="#drawable/shape_7" />
<item android:state_pressed="false" android:drawable="#drawable/transparent_shape" />
</selector>
and a shape is defined here:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF4FDBD5"
android:endColor="#FF1AA6A1"
android:type="linear"
android:angle="90"/>
</shape>
or in the other way when you construct the ListView items you can assign the selector to the 'convertView'
cheers
For your id attribute, change it to android:id="#android:id/list. This is the default ID for the ListView.
Try this selector. It works for me
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_selected="true" android:drawable="#android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#drawable/list_item_back" />
</selector>

Categories

Resources