3

I have a listview and i want to when i click on a row, its background changes to blue. i use this code:

listView1.setOnItemClickListener(new OnItemClickListener() {
                        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
                            // TODO Auto-generated method stub
                            parent.getChildAt(position).setBackgroundColor(Color.BLUE);
                        }

                    });

this works wit some wrong. when i click on first item, it turns to blue but item #3 and #5 changes to blue too!!! i cant understand why!! i just want only selected item turns to blue!!!

Fcoder
  • 9,066
  • 17
  • 63
  • 100

3 Answers3

2

What about to use selectors? They works properly and provide clean solution.

listselector.xml

<?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/normal" />
 
    <item
        android:state_selected="true"
        android:state_focused="false"
        android:drawable="@drawable/hover" 
        />
    
    <item 
        android:state_selected="true"
        android:state_pressed="false"
        android:drawable="@drawable/hover" />
</selector>

normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="#cccccc"
        />
</shape>

hover.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="#dddddd"
        />
</shape>

An Usage

<ListView
   android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:listSelector="@drawable/listselector"
/>

Key property is to set android:listSelector="@drawable/listselector" for your ListView.

Note:

You can use in shape also gradient property instead of solid color. And for more details also you can look at tutorial Android Custom ListView.

Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • it works but when i tap on a row color change but when i focus out from row color resets. i want color change permanently. how i can do this – Fcoder Mar 15 '13 at 18:34
  • Hi, Still the background is changed on item click only. And I am using custom adaptor how can i change the selected item-view textview color??? – Sampath Kumar May 17 '13 at 04:05
0

If you have a custom list view then use the below code.

  public View getView(final int arg0, View arg1, ViewGroup arg2) {
final ViewHolder vh;
vh= new ViewHolder();

if(arg1==null )
{
                arg1=mInflater.inflate(R.layout.lyourcustomlayouttobe inflated, arg2,false);//custom layout inflated
        arg1.setTag(vh);
        }

return arg1;

}

Your custom layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="horizontal"
 android:cacheColorHint="#000000"
 android:background="@drawable/listviewbkg">
 //other items to be inlfated.
 </LinearLayout>

Create a drawable folder under resources. post the below xml as listviewbkg

 <?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/pressed" />
 <item  android:state_focused="false" 
 android:drawable="@drawable/normal" />
 </selector>

Shape when normal under drawable with name normal.xml

   <?xml version="1.0" encoding="UTF-8"?> 
   <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
   <solid android:color="#FFFFFF"/>//change color    
    <stroke android:width="3dp"
    android:color="#0FECFF" /><!-- #330000FF #ffffffff -->//border color
   <gradient                               // remove the gradient if do not wish to use.
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

    <padding android:left="5dp"
     android:top="5dp"
     android:right="5dp"
     android:bottom="5dp"/> 
    <corners android:bottomRightRadius="7dp"      // change this to increase the rounded edge radius
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
     </shape>

Shape when pressed under the name pressed.xml in drawable folder

 <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#FF1A47"/>    //change color 
  <stroke android:width="3dp"
    android:color="#0FECFF"/>//border color
  <padding android:left="5dp"
     android:top="5dp"
     android:right="5dp"
     android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"// increase the radius at the edge
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
  </shape>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

use listselectors to achieve this goal:

Hree is an example: http://www.michenux.net/android-listview-highlight-selected-item-387.html

and if you want your listview item color permanent, then, you need to create an array of selected positions, an in getview() method of your cutom adapter, you need to check if that position is exists in array or not, if yes, then change background color of your view manually

Akbari Dipali
  • 2,173
  • 1
  • 20
  • 26