Ask Your Question
0

How can the highlight on listView click be disabled in Android?

asked 2022-08-01 11:00:00 +0000

nofretete gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-24 15:00:00 +0000

qstack gravatar image

To disable the highlight on ListView click, you can use the following code snippet:

  1. First, create a new XML file in the res/drawable folder named list_selector.xml with the following code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/transparent" />
</selector>
  1. In your ListView element in the activity's XML file, add the following attribute:
android:listSelector="@drawable/list_selector"

This will set the list selector to our custom list_selector.xml file, which is transparent and will not show any highlight when the user touches an item in the ListView.

Alternatively, you can also disable the highlight programmatically in the ListView's OnItemClickListener by adding the following code:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Disable highlight
        view.setBackgroundColor(Color.TRANSPARENT);

        // Perform item click action
        // ...
    }
});

This will set the background color of the clicked view to transparent, effectively disabling the highlight for that item.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-08-01 11:00:00 +0000

Seen: 11 times

Last updated: Jul 24 '22