0

I have got an custom list view with an longclicklistener. i would like to set an row selection color if I tap on any row of ListView. at the moment no color will be show for selection

this is my listView:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listViewProducts"
    android:divider="#dddddd"
    android:dividerHeight="2dp"
    android:smoothScrollbar="true"
    android:stackFromBottom="false"
    android:layout_alignParentLeft="false"
    android:layout_alignParentStart="false"
    android:clickable="true"
    android:focusable="true"
    android:fastScrollAlwaysVisible="false"
    android:background="@drawable/list_selector"/>

ListViewRowItem

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/row_bg"
android:paddingTop="5dp"
android:paddingBottom="5dp">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtView1"
    android:layout_alignParentTop="true"
    android:textSize="11sp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="20dp"
    android:focusable="false"
    android:clickable="false"
    android:layout_marginTop="3dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtView2"
    android:layout_below="@+id/txtView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="13sp"
    android:layout_marginLeft="20dp"
    android:textColor="@color/DefaultGreen"
    android:textStyle="bold"
    android:focusable="false"
    android:clickable="false"/>

<View
    android:layout_width="2dp"
    android:layout_height="50dp"
    android:background="#e3e3e5"
    android:layout_marginRight="70dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="70dp"
    android:focusable="false"
    android:clickable="false"
    android:id="@+id/Seperator" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:id="@+id/txtView3"
    android:textSize="13sp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignLeft="@+id/Seperator"
    android:layout_alignStart="@+id/Seperator"
    android:gravity="center_vertical|center_horizontal"
    android:textColor="@color/DefaultGreen"
    android:textStyle="bold"
    android:focusable="false"
    android:clickable="false"/>

Trombone0904
  • 4,132
  • 8
  • 51
  • 104

3 Answers3

1
 ListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                view.setBackgroundColor(FragementView.getResources().getColor(R.color.colorGreen));
                return false;
            }
        });

check the above code for changing the color on long press

Manoj kumar
  • 450
  • 4
  • 13
0

then dont use the onlongclick listner in java use the selectors in xml.

add the list_selector.xml in drawable

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

    <item android:drawable="@color/colorGreen" android:state_pressed="true"/>
    <item android:drawable="@color/colorwhite" android:state_focused="true"/>
    <item android:drawable="@android:color/white"/>

</selector>

add the color.xml in values folder

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="colorblack">#000000</color>
    <color name="colorwhite">#ffffff</color>
    <color name="colorGreen">#55C243</color>

</resources>

and add the listSelector to the listview:

 <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@drawable/list_selector" >
    </ListView>

enter image description here see this is onclick of it

Manoj kumar
  • 450
  • 4
  • 13
0

I kept android:background="@drawable/list_item_selector" in main view of list row and it is working for me.

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@color/red" android:state_pressed="true"/>
<item android:drawable="@color/red" android:state_focused="true"/>
<item android:drawable="@color/gray"/>

this is my selector

Srikanth
  • 1,555
  • 12
  • 20
  • if i but this unter android:background, the background color of my whole list view will be change but not the selected row – Trombone0904 Dec 09 '15 at 10:20
  • you need to keep it in main view of 'list row' layout not in'list layout' – Srikanth Dec 09 '15 at 10:34
  • i added my list view.xml and my listviewrowitem.xml - what is my mistake? – Trombone0904 Dec 09 '15 at 10:40
  • add it only to you ListViewRowItem layout file – Spiri Dec 09 '15 at 11:03
  • and where there? android:background="@drawable/list_item_selector" into the relative layout? if yes, this i tried, too -> nothing – Trombone0904 Dec 09 '15 at 11:14
  • maybe it would help if you update your post by adding the 'list_item_selector' xml file content and the color codes you're using for the listview/listviewitem layout files, because I got it working on my side using your 'ListViewRowItem' and 'listView' xml layouts. Also, I've noticed that you have 'android:background="@drawable/list_selector" in the list view xml file - is that needed? – Spiri Dec 09 '15 at 11:29
  • i found the solution. all your ways are correct. it was my mistake, AGAIN! :D i set the background color in the activity programmatically for testing – Trombone0904 Dec 09 '15 at 11:31