AndroidOptimist
AndroidOptimist

Reputation: 1449

Highlight a row in listview

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..

Upvotes: 1

Views: 653

Answers (2)

Developer
Developer

Reputation: 6350

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"

Upvotes: 1

Janoti
Janoti

Reputation: 36

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

Upvotes: 0

Related Questions