1

I am creating a listview with the reference from this link. Now am trying to highlight a particular row in listview. When am pressing the button the particular row got highlighted for a second. But what i want is that row should stay in same color until i presses the button in next row. my listview is

<ListView
        android:id="@+id/mainListView"
        android:layout_width="154dp"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:background="@drawable/layer_list"
        android:dividerHeight="2px"
        >

and in listview am placing a textview and an imageview. For highlighting the view i followed this tutorial. Am new to android. Help me in achieving this. Thanks in advance..

AndroidOptimist
  • 1,419
  • 3
  • 23
  • 38

2 Answers2

1

Create A Selector in the Draw-able Folder listview_item_selection_effect.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true">
    <shape>
        <solid android:color="#ffffff" />
    </shape>
</item>
    <item>
    <shape>
        <solid android:color="#00a7eb" />
    </shape>
</item>
</selector>

In Your Layout which has the ListView

android:background="?android:attr/activatedBackgroundIndicator"

In Your Activity On ListView item Clicked

   listView.setOnItemClickListener(new OnItemClickListener(){
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3){ 
       listView.setSelector(R.drawable.listview_item_selection_effect);
    listView.setItemChecked(position,true);
    }
});

In the ListView

android:choiceMode="singleChoice"
Developer
  • 6,292
  • 19
  • 55
  • 115
  • 1
    Position is the Item that is selected in the listview – Developer Sep 04 '13 at 09:57
  • Thanks for your response. But i don't want to mention the position previously means on runtime when am pressing the up button listview should highlight the particular row until i press the next one. – AndroidOptimist Sep 04 '13 at 10:04
  • what is the problem your are now facing,this is working perfectly for me if u can send me u r sample project i can resolve this issue – Developer Sep 04 '13 at 10:31
  • If am putting choicemode in listview it is not working friend. But without that it is working perfectly.. Thank u friend :-) .. – AndroidOptimist Sep 04 '13 at 10:45
0

a) Use a custom layout

    MyLayout extends LinearLayout implements Checkable {
    boolean check = false
    public void setChecked(boolean checked) {
          if (checked){
             //HIGHLIGHT THE BACKGROUND
          }{
            // remove the background
          }

    }
    public void toggle() {
        setChecked(!check);
    }
    }

b )In custom listview item use MyLayout as layout

c )In your activity implement OnItemClickListener and in onItemClick call listview.setItemChecked(position, true);

Vedang Jadhav
  • 510
  • 2
  • 11
Janoti
  • 36
  • 5