Ayush Goyal
Ayush Goyal

Reputation: 2079

Set color of selected item in ListFragment?

My OnActivityCreated:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

and onListItemClick:

public void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        DetailFragment fragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detailFragment);
        if (fragment != null && fragment.isInLayout()) {
            v.setBackgroundColor(getResources().getColor(R.color.darkcherry));
            fragment.setText(item);
        }

By doing this, the selected item gets the colored background but then selecting the another item the previous selection doesn't go away. In my previous question the solutions provided use xml but I haven't defined the xml for listview, only fragments are defined. How do I color the selection(background)?

Link to previous question : How to set color of selected item in ListFragment in android?

Upvotes: 1

Views: 4222

Answers (4)

iceman
iceman

Reputation: 826

Try using a custom adapter, and in the layout of your list item, set a selector as the background drawable.

Upvotes: 0

rajpara
rajpara

Reputation: 5203

In the sample of ListFragment in support4 library, they achieve this kind of functionality by making list row layout as checkable.

Please find my answer and demo project.

Upvotes: 1

pablisco
pablisco

Reputation: 14237

You need to keep track of the selected view so you can change the color of it when selecting the new one.

Also, keeping this variable will allow you to keep track of the selection when the screen is rotated (saving and restoring the state of the fragment).

Alternatively, if you are using the selector property of the list view, you can set is as single choice mode:

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

on creation of the fragment. Obviously, you'll have to remove the onListItemClick.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

Use a selector as background for your custom row:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/selected_row" />
    <item android:drawable="@drawable/normal_drawable" />
</selector>

and when you click on an element of your list, set the status of the row you clicked as selected:

public void onListItemClick(ListView l, View v, int position, long id) {

        v.setSelected(true);

        String item = (String) getListAdapter().getItem(position);
        DetailFragment fragment = (DetailFragment) getFragmentManager()
                .findFragmentById(R.id.detailFragment);
        if (fragment != null && fragment.isInLayout()) {
            v.setBackgroundColor(getResources().getColor(R.color.darkcherry));
            fragment.setText(item);
        }

Upvotes: 0

Related Questions