Home > Blockchain >  How to change a backgound of a button in recyclerview
How to change a backgound of a button in recyclerview

Time:10-13

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();
    }
}

CodePudding user response:

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

  • Related