Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView does not Highlight when a OnClickListener is set

I have a ListView populated with custom XML ListItems, this is the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/listPreferredItemHeight"
                android:padding="6dip">
...
</RelativeLayout>

The listView shows correctly on screen and if i click or hold on an item it becomes blue (I'm using Holo Light Theme)

the problem comes when i try to assign an OnClickListener to the view, inside getView in my Activity that extends BaseAdapter:

@Override
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
    ...
    convertView = InflateUtils.inflate(mContext, R.layout.list_item);
    ...
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(mContext, "Test", 2000).show();
        }
    });
...
}

after doing that, the list item highlight color is no more shown, when i click or hold on a list item it's background stays white, anyway the onClickListener is perfectly working.

Do you have any suggestion to get the highlight color while keeping the default styles of HoloLight?

like image 808
Luca Vitucci Avatar asked Oct 22 '22 03:10

Luca Vitucci


1 Answers

You may want to use OnItemClickListener on the ListView itself, instead of separate click listeners for item views.

Also, highlight problem has already been described here: Android listview no longer highlights selection onclick

like image 165
Ivan Bartsov Avatar answered Oct 24 '22 05:10

Ivan Bartsov