How to make listview selected item selectable? - android

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>

Related

Why is this ripple effect is not working as expected?

I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />

dont work select single item and highlight listview 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.

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 :)

Why changes ListView background when list item click on android 2.3.3

I have a problem with ListView background on android 2.3.3 (android 4 no problem). When click on list item, changes ListView background to #color/holo_green from android:listSelector attribute. How to resolve this problem?
my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
android:alwaysDrawnWithCache="false"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#drawable/sa_list_item_selector"
android:divider="#drawable/sa_list_divider"/>
Adding android:background="#android:color/transparent" nothing changes
sa_list_item_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/holo_green" />
<item android:drawable="#android:color/transparent" />
</selector>
Maybe try to change:
<?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/holo_green" />
<item android:drawable="#android:color/transparent" />
</selector>
To:
<?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/holo_green" />
</selector>
Look at your selector. The code is there.

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