-1

I made a recyclerview and there is a button on myitem. I want to change its color when I click it. How can I do that?

This is my adapter code:

package com.example.livraison;

    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;

    import java.util.ArrayList;

    public class ListAdapter extends RecyclerView.Adapter<ListViewHolder> {
    ArrayList<ListModel> data;
    Context context;

    public ListAdapter(ArrayList<ListModel> data,Context context){
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = 
                LayoutInflater.from(context).inflate(R.layout.item_produit_liverer,viewGroup,false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int position) {
        listViewHolder.command_i.setText(data.get(position).getCommand_m());
        listViewHolder.name_i.setText(data.get(position).getName_m());
        listViewHolder.commune_i.setText(data.get(position).getCommune_m());
        listViewHolder.providence_i.setText(data.get(position).getProvidence_m());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [android set button background programmatically](https://stackoverflow.com/questions/13842447/android-set-button-background-programmatically) – Ryan M Oct 14 '21 at 08:06

1 Answers1

0

listViewHolder.button.setBackgroundColor(ContextCompat.getColor(context, R.color.color_name));

kashyap
  • 498
  • 1
  • 6
  • 21
  • i have another problem i want to if i click to button the color of button will be red and if i click on the button again the color will be green i write this code and it doesn't work: – Bakir Lakaf Oct 10 '21 at 22:18
  • listViewHolder.p_i.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { boolean click; if (click=false){ listViewHolder.p_i.setBackground(ContextCompat.getDrawable(context,R.drawable.rouge)); return true; }else if (click=true){ listViewHolder.p_i.setBackground(ContextCompat.getDrawable(context,R.drawable.vert)); – Bakir Lakaf Oct 10 '21 at 22:19
  • return false; } return true;} }); – Bakir Lakaf Oct 10 '21 at 22:19