1

I have a RecylerView which contains a textView as individual item. Based on the requirement, the background image of the textView will be changed on click after checking the present image.

In simple:

onClick textView TV--> check for the background image of the textview TV--> check if the background image of TV is a, then make it b and viceversa

Please find my code below:

 viewHolder.addRemove.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(viewHolder.clicked==0)
                            {

                                viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
                                viewHolder.clicked=1;
                                groupSlcArray.add(model.getName().trim());
                                Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));

                            }
                            else
                            {

                                viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
                                viewHolder.clicked=0;
                                groupSlcArray.remove(model.getName().trim());
                                Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
                            }

                        }
                    });

I am unable to find anyway to get the background resource of the textView for checking.Need your help.This question might seem too simple for some. So please before Outvoting, try to give a solution because I have tried all the possible solutions that I found.Thanks in advance

Animesh Jena
  • 1,541
  • 1
  • 25
  • 44

3 Answers3

0

Try following code :

holder.textview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if (holder.textview.getBackground() == getResources().getDrawable(R.drawable.a)){
                holder.textview.setBackground(R.drawable.b);
           }
           else{
                holder.textview.setBackground(R.drawable.a);
           }
        }
    });
R.R.M
  • 780
  • 4
  • 10
0

Here is a solution that you can use for change background image of each RecyclerView item.

For example, currently, each row of your RecyclerView is a Item, you need to create 1 more field name textViewBackGround for hold the back ground of each row like this

public class Item{
    ...
    public int textViewBackGround = R.drawable.a; // default image a
}

Then in your adapter

public class MyRecyclerViewAdapter extends RecyclerView.Adapter < MyRecyclerViewAdapter.CustomViewHolder > {
    private List < Item > itemList;
    private Context mContext;

    public MyRecyclerViewAdapter(Context context, List < Item > itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @Override
    public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
        Item item = itemList.get(i);
        viewHolder.textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // don't need to get the background of your TextView then change the background, just need to check the datasource. Later on, you absolutely will know that check the datasource for change background of item is a correct way not check the background resource
                if (item.textViewBackGround == R.drawable.a) {
                    viewHolder.textView.setBackgroundResource(R.drawable.b);
                    item.textViewBackGround = R.drawable.b;
                } else {
                    viewHolder.textView.setBackgroundResource(R.drawable.a);
                    item.textViewBackGround = R.drawable.a;
                }
            }
        });
        viewHolder.textView.setBackgroundResource(item.textViewBackGround); // need to set the background image for `TextView` everytime bind ViewHolder because RecyclerView item will recycle when you scroll (if you don't understand it, you can remove this line, run your app, scroll up/down and you will understand it)  
        ...
    }

    class CustomViewHolder extends RecyclerView.ViewHolder {
        protected TextView textView;

        public CustomViewHolder(View view) {
            super(view);
            ...
        }
    }
}
Linh
  • 57,942
  • 23
  • 262
  • 279
  • Thank you for the solution. Please check my codes above. Similar to textViewBackGround , I have used a clicked variable. But There are some issues for which I can't use conditions like that. – Animesh Jena May 05 '17 at 06:25
0

You have to understand about getView. Sometimes If you scroll in ListView, This is not an exact Color

So I would like to recommand following ways like this:

1. Add Code on getView

if (groupSlcArray.contain(odel.getName().trim())) {
        viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
    } else {
        viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
    }

2. Change OnClickListener Event Code

viewHolder.addRemove.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View v){
    if (groupSlcArray.contain(odel.getName().trim())) {
        viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
        groupSlcArray.remove(model.getName().trim());
        Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));

    } else {
        viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
        groupSlcArray.add(model.getName().trim());
        Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
    }
  }
});
redAllocator
  • 725
  • 6
  • 12