Search code examples
androidandroid-recyclerviewonclickonclicklistener

Change text and background on clicks in RecyclerView


I have a RecyclerView and want to change text and item background when it is clicked. And change it back to the initial state when clicked again. Initial text and background is set by default in XML.

Please write in comments if you need to see more code.


WindowAdapter.java

public class WindowAdapter extends RecyclerView.Adapter<WindowAdapter.WindowViewHolder> {

    private Context mCtx;
    private List<Window> windowList;

    public WindowAdapter(Context mCtx, List<Window> windowList) {
        this.mCtx = mCtx;
        this.windowList = windowList;
    }

    @NonNull
    @Override
    public WindowViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.list_layout, null);
        return new WindowViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull WindowViewHolder holder, int position) {

        Window window = windowList.get(position);

        holder.textViewTitle.setText(window.getTitle());
        holder.textViewChecked.setText(window.getCheck());

        holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(window.getImage()));
////////////////////////////////////////////////
        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(mCtx, "Clicked", Toast.LENGTH_SHORT).show();


            }
        });
//////////////////////////////////////
    }

    @Override
    public int getItemCount() {
        return windowList.size();
    }

    class WindowViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textViewTitle;
        TextView textViewChecked;

        RelativeLayout parentLayout;

        public WindowViewHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            textViewTitle = itemView.findViewById(R.id.window_name);
            textViewChecked = itemView.findViewById(R.id.window_check);
///////////////////////////////////////////
            parentLayout = itemView.findViewById(R.id.parent_layout);
        }
    }
}

Solution

  •  boolean isBackgroundChange = false;
    
     public void onBindViewHolder(@NonNull WindowViewHolder holder, int position) {
    
            Window window = windowList.get(position);
    
              holder.textViewTitle.setText(window.getTitle());
          //  holder.textViewChecked.setText(window.getCheck());
          //  holder.imageView.setImageDrawable(mCtx.getResources()
          // .getDrawable(window.getImage()));
    
            holder.parentLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                if(!isBackgroundChange){
                holder.textViewChecked.setText("Button Clicked");
                holder.parentLayout.setBackground(....); //set Background for RelativeLayout after button clicked
                holder.imageView.setImageDrawable(......); // set the image which you want to show after button clicked.
                isBackgroundChange = true;
                }else{
                holder.parentLayout.setBackground(....); // set previous background for realtiveLayout.
                holder.textViewChecked.setText("Button Clicked again ");
                holder.imageView.setImageDrawable(.....); // set the previous image.
                isBackgroundChange = false;
    
                }
    
                }
            });
    
        }