Search code examples
androidbuttonandroid-recyclerviewtextcolor

I would like to change Button TextColor in RecyclerView


I would like to change the color when I click the Button in RecyclerView.

I want to change the text color of the 4 button when I click the button corresponding to position 4 of position 1, 2, 3, 4.

public class SingleAdapter extends RecyclerView.Adapter<SingleAdapter.ViewHolder> {

public interface OnItemClickListener {
    void onItemClick(int position);
    void onLongItemClick(int position);
}

Context context;
List<SingleItem> items;
public static OnItemClickListener onItemClickListener;


public SingleAdapter(Context context, List<SingleItem> items) {
    this.context = context;
    this.items = items;
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener)
{
    this.onItemClickListener = onItemClickListener;
}

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

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.single_item, parent, false);
    final ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
    SingleItem item = items.get(position);
    viewHolder.btnItemButton.setText(item.getmSubText());
}


static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    Button btnItemButton;

    public ViewHolder(@NonNull final View itemView) {
        super(itemView);
        btnItemButton = itemView.findViewById(R.id.itemButton);

        btnItemButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(onItemClickListener != null)
        {
            onItemClickListener.onItemClick(getAdapterPosition());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return false;
    }
}

}

public class SingleItem {

String mSubText;

public SingleItem(String mSubText)
{
    this.mSubText = mSubText;
}

public String getmSubText() {
    return mSubText;
}

public void setmSubText(String mSubText)
{
    this.mSubText = mSubText;
}

}

static String mAddListDepthUnits[] = { "m", "ft", "fm", "HR", "pb" };



private void setDepthRecycler()
{
    singleItems = new ArrayList<>();
    for( int i = 0; i < mAddListDepthUnits.length; i++)
    {
        singleItems.add(new SingleItem(mAddListDepthUnits[i]));
    }

    mSingleAdapter = new fec.fishfinderapp.menu.SingleAdapter(mSubMenu_Units.getContext(), singleItems);
    midDepthRecycler.setAdapter(mSingleAdapter);

    mSingleAdapter.setOnItemClickListener(new SingleAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            switch(position)
            {
                case 0 :
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                case 5 :
                    break;
            }

        }

        @Override
        public void onLongItemClick(int position) {

        }
    });
}

The current code is in this state. How can I change the text color of a specific button here? I can hardly find the answer. Please help me


Solution

  • As my understanding you need to change the text color based on button click?

    @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
            SingleItem item = items.get(position);
            viewHolder.btnItemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(position == 4) {
                        viewHolder.btnItemButton.setTextColor(Color.RED);
                    }else{
                        viewHolder.btnItemButton.setTextColor(Color.GREEN);
                    }
                }
            });
    
    
            viewHolder.btnItemButton.setText(item.getmSubText());
        }