Praneeth
Praneeth

Reputation: 1260

How to Highlight a ListItem in a list view?

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

Upvotes: 3

Views: 74

Answers (3)

user5366495
user5366495

Reputation:

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

Upvotes: 0

iamabhaykmr
iamabhaykmr

Reputation: 2011

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

Upvotes: 1

Yongho
Yongho

Reputation: 21

try case of state_selected is false

<item android:state_pressed="false" android:drawable="...."/>

Upvotes: 1

Related Questions