Search code examples
androidlistviewsharedpreferencesbackground-color

How can I set and save background color for some item in the ListView, when the user clicks on it?


I would like to change some item background color in my listview, when the user clicks on it. Furthermore, I want to save this in the shared preferences. I useing adapter for ListView. How can I do ?

My CategoryAdapter class:

public class CategoryAdapter extends ArrayAdapter<Category> {

    private LayoutInflater mInflater;
    private int mResource;

    public CategoryAdapter(Context context, int resource, List<Category> categories) {
        super(context,resource,categories);

        mResource = resource;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent )
    {
        View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;
        TextView categoryTitle = (TextView) view.findViewById( R.id.name );

        Category item = getItem( position );

        categoryTitle.setText( item.getCategoryTitle() );

        return view;
    }

}

Where I want to use it:

CategorylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

             }
        });

CategorylistView:

CategorylistView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,category){
           @Override
           public View getView(final int position, final View convertView, final ViewGroup parent) {
 if(catViewPreferences.getInt("scn",0)==position){
                   textView.setBackgroundColor(Color.BLUE);
               }
return textView;
           }
       });

OnItemClickListener:

CategorylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {

                final String category = (String) adapterView.getItemAtPosition(i);
                final String email = loginPreferences.getString("email","");
                final String password = loginPreferences.getString("password", "");

                final int position =  adapterView.getSelectedItemPosition();

                if(i<8) {
                    view.setBackgroundColor(getResources().getColor(R.color.colorLightBlue));
                }

                catViewPrefEditor.putInt("scn", position);
                catViewPrefEditor.commit();

Solution

  • You can do it by changing the background of the view on onItemClick method:

    public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
              view.setBackgroundColor(Color.BLUE);
         }
    

    UPDATE: If you want to deselect items, you can change the top method like this:

    public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
              if(((ColorDrawable)view.getBackground()).getColor() == Color.Blue){
                  //the item is selected...now u must deselect it
                  view.setBackgroundColor(Color.WHITE);
              } else {
                  view.setBackgroundColor(Color.BLUE);
              }
         }
    

    I hope this helps you.