3

I want to highlight a Particular row item in list view to show as selected Item.

I have a list of countries I want highlight a single country so that it will be selected by a button.

I used this:

<ListView
    android:id="@+id/list_country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/doneBtn"
    android:choiceMode="singleChoice"
    android:listSelector="@drawable/list_selector"
    android:layout_below="@+id/image_logo">
</ListView>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/list_selector">
    <TextView
        android:id="@+id/country_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_gravity="center"
        android:text="CountryName"
        android:textSize="24sp"/>

</LinearLayout>



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        mCountryAdapter = new CountryAdapter(this, R.layout.single_country, countryList);
        mListViewCountry.setAdapter(mCountryAdapter);
        mListViewCountry.setSelector(R.drawable.list_selector);
        clickListeners();
    }

here is my selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/color_selector"/>
    <item android:state_selected="true" android:drawable="@color/color_selector"/>

</selector>

It is just changing color when I click or press on the item and if I remove touch, It is changing to default color! can any one help me what i'm missing

Praneeth
  • 1,260
  • 18
  • 37
  • this can be done by this [enter link description here](http://stackoverflow.com/questions/39696340/accessing-listview-checkbox-in-android/39697518#39697518) – kggoh Oct 07 '16 at 06:56

3 Answers3

1

try case of state_selected is false

<item android:state_pressed="false" android:drawable="...."/>
Yongho
  • 21
  • 5
1

You can use convertView.setAlpha(0.8f); to make it little transparent to differentiate between selected and not selected items

iamabhaykmr
  • 1,803
  • 3
  • 24
  • 49
0

Check this ViewSelector I made. Its very simple to use and can be applied to any view.

ViewSelector viewSelector = new ViewSelector();

viewSelector.onClickColorSelector(relativelayout, Color.RED, 0);

Just pass a halv transparent color in the argument