1

I need to set text color in adapter class not on activity and using color value that record in colors.xml file !

and this is the code: sorry for any missing things and this is the code: sorry for any missing things and this is the code: sorry for any missing things

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
private final LayoutInflater inflater;
ArrayList<HashMap<String, String>> productsHashMapList;

/*int[] images = {
        R.drawable.ic_launcher_foreground,
        R.drawable.ic_launcher_background,
        R.drawable.ic_launcher_foreground
};*/

public MyAdapter(Context context, ArrayList<HashMap<String, String>> productsJsonList){
    inflater = LayoutInflater.from(context);
    this.productsHashMapList = productsJsonList;

}

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

    View view = inflater.inflate(R.layout.list_row,parent,false);
    myViewHolder holder = new myViewHolder(view);

    return holder;
}

@Override
public void onBindViewHolder(myViewHolder holder, int position) {
    holder._productName.setText(productsHashMapList.get(position).get("name"));
    holder._productName.setTextColor(getResources().getColor(R.color.colorRed));

}

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

public class myViewHolder extends RecyclerView.ViewHolder {

    ImageView   _imgview;
    TextView    _productName;


    public myViewHolder(View itemView) {

        super(itemView);
        _imgview            = (ImageView) itemView.findViewById(R.id.logo);
        _productName           = (TextView) itemView.findViewById(R.id.productName);
    }
}

}

Anas
  • 97
  • 3
  • 9

4 Answers4

2

First of all pass the context through the constructor of the adapter from Activity while initializing the adapter in activity and set context in the adapter:

this.context = context; // set this in the constructor.

Then, set the textColor:

textView.setTextColor(context.getResources().getColor(R.color.white)); //whatever your color
Prashanth Verma
  • 588
  • 3
  • 11
1

Try with

textViewOBJ.setTextColor(ContextCompat.getColor(context, R.color.your_color_code));

FYI

You should pass Context.

How?

Through your Adapter class. #Contsructor

 ArrayList<HashMap<String, String>> productsHashMapList;
   Context context;

public MyAdapter(Context contextOBJ, ArrayList<HashMap<String, String>> productsJsonList){
    this.context=contextOBJ;
    inflater = LayoutInflater.from(context);
    this.productsHashMapList = productsJsonList;

}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

textView.setTextColor(getResources().getColor(R.color.color));

as per prashanth verma anser, initailize context in contsructor. Then use it in code.

textView.setTextColor(context.getResources().getColor(R.color.color));

Naveen
  • 350
  • 2
  • 12
0

Always set the text color inside your RecyclerView.ViewHolder class, not inside the onBindViewHolder method of the adapter class, have a look on the below solution

class MyViewHolder extends RecyclerView.ViewHolder {
 private TextView YOUR_TEXT_VIEW;
        MyViewHolder(View view) {
            super(view);
        YOUR_TEXT_VIEW = view.findViewById(R.id.YOUR_TEXT_VIEW);

        YOUR_TEXT_VIEW.setTextColor(ContextCompat.getColor(YOUR_APP_CONTEXT, R.color.your_color_code));

        }


    }
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103