0

i want to highlight selected row with different color in listview. Here i using CustomAdapter for populating rows in listview. When i click any row then change the background of selected row. If i select another the highlight selected one, remove previous highlighted background.

relation_dialog.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"
    android:background="#FFFFFF">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">       

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="33dp"
                    android:text="Done"
                    android:id="@+id/btnDoneRelationDialog"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/buttonshape"
                    android:textColor="#FFFFFF"
                    android:textAllCaps="false"/>


        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/popup_lstview_relations"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_above="@+id/relativeLayout"
            android:choiceMode="singleChoice"/>
    </RelativeLayout>
</LinearLayout>

AddMemberSearch.java

 listViewPopup = (ListView) d.findViewById(R.id.popup_lstview_relations);

        RelationAdapter relationAdapter = new RelationAdapter(AddmemberSearch.this, relationList);
        listViewPopup.setAdapter(relationAdapter);

        listViewPopup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
               // listViewPopup.getSelectedView().setBackgroundColor(getResources().getColor(R.color.title_bar_color));
                Object o = listViewPopup.getItemAtPosition(position);
                relValue = ((ValueNamePairBO) o).getValue();
                Log.d("System Out", "relValue: " + relValue);
                view.setSelected(true);
                autoComplteVerifyUserId.setText("");
            }
        });

RelationAdapter class

    public class RelationAdapter extends BaseAdapter {
    private ArrayList<ValueNamePairBO> listData;
    private LayoutInflater layoutInflater;
    public RelationAdapter(Context aContext, ArrayList<ValueNamePairBO> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.relation_row, null);
            holder = new ViewHolder();

            holder.txtRelationName = (TextView) convertView.findViewById(R.id.txtRelationName);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



        holder.txtRelationName.setText(listData.get(position).getName());
        return convertView;
    }

    static class ViewHolder {
        TextView txtRelationName;
    }

}

relation_row.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="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/txtRelationName"
        android:layout_gravity="left"
        android:textColor="#000000"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:paddingLeft="20dp"
        />
</LinearLayout>
  • 3
    possible duplicate of [Highlight selected item in ListView on Android](http://stackoverflow.com/questions/22154358/highlight-selected-item-in-listview-on-android) – Bö macht Blau Sep 24 '15 at 06:32
  • issue resolved please check @rajan ks answer...it is simply good... –  Oct 01 '15 at 07:02

3 Answers3

2

create drawable/listview_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >

    <solid android:color="#e4e4e4" />

</shape>

create drawable/select_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >

    <solid android:color="#6f89cff0" />

</shape>

create drawable/list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/select_bg" android:state_pressed="true"/>
    <item android:drawable="@drawable/select_bg" android:state_selected="true"/>
    <item android:drawable="@drawable/listview_bg"/>

</selector>

now add the listSelector on your listview

  <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/popup_lstview_relations"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/relativeLayout"
        android:choiceMode="singleChoice"
        android:listSelector="@drawable/list_selector"/>
arun
  • 1,728
  • 15
  • 15
1

use android:listSelector and set color you want to Highlight

   <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/popup_lstview_relations"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_above="@+id/relativeLayout"
                android:choiceMode="singleChoice"
                android:listSelector="@android:color/darker_gray"/>
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
0

You need to have a state drawable as list selector. You can either extend from Listview widget in your styles or set it by yourself.

The default is:

 <item name="android:listSelector">?attr/listChoiceBackgroundIndicator</item>

In your example you should write:

android:listSelector="?attr/listChoiceBackgroundIndicator"

And instead of setting the view selected in your onitemclicklistener you should set activated.

view.setActivated(true);
Thomas R.
  • 7,988
  • 3
  • 30
  • 39