Home > Back-end >  How i can make the color of button changed when i click on and when i click on again the color retur
How i can make the color of button changed when i click on and when i click on again the color retur

Time:10-11

How i can make the color of button changed when i click on and when i click on again the color return to the first color i work with RecyclerView and this is my adapter code :

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());
    listViewHolder.l_i.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            listViewHolder.l_i.setBackgroundColor(R.drawable.vert);
        }
    });

}

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

CodePudding user response:

private String DEFAULT = "#000000"

private String WHITE = "#ffffff"

listViewHolder.name_i.setTag("default")

if (listViewHolder.name_i.getTag().equals("white")){
            listViewHolder.name_i.setTag("default");
            listViewHolder.name_i.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(DEFAULT)));

        }else{
            listViewHolder.name_i.setTag("white");
            listViewHolder.name_i.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(WHITE)));

        }

CodePudding user response:

Try

setBackgroungTintList(new ColorStateList.valueOf(getColor(R.color.your_color))) 
  • Related