Search code examples
androidbackground-color

Change background colour of selected listview items permanently untill another items get selected


I want to change background colour of selected item permanently until another item selected. I've tried many answered on stackoverflow but not get required solution. I'm using list Fragment. Code of this,

public class LeftFilterContents extends ListFragment implements AdapterView.OnItemClickListener {

    Get get;
    View view;
    ListView listView;
    AdapterLeftFilterContents adapter;

    public interface Get {
        void getData(int s);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            get = (Get) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.list_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<String> arrayList = new ArrayList<>();
        listView = (ListView) view.findViewById(android.R.id.list);

        arrayList.add("Type");
        arrayList.add("Date");
        arrayList.add("From to Date");

        adapter = new AdapterLeftFilterContents(getActivity(), R.layout.single_list_item_view, arrayList);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        get.getData(position);
    }
}

list_fragmnet.xml

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:listSelector="#666666">
    </ListView>

single_list_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/list_item_left_filter_contents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Solution

  • All you need to do is provide a selector to either the TextView or the parent LinearLayout.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/my_list_selector">
        <TextView
            android:id="@+id/list_item_left_filter_contents"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    Create a new Drawable Resouce File in your app's drawable folder.

    my_list_selector.xml

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

    control_bar_gradient.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <gradient
    
            android:angle="90"
            android:type="linear"
            android:startColor="#7e0809"
            android:endColor=  "#fa0000" />
    </shape>
    

    And finally simply select the clicked view and unselect the previous ones.

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        if (prevSelectedView != null
            prevSelectedView.setSelected(false);
        view.setSelected(true);
        prevSelectedView = view;
    
        get.getData(position);
    
        // Here provide position to the adapter to update views in case of scrolling
    }