0

Hi guys please I'm working on an app that set each item background color at run time, and my default text color is black but if the background is also black I can't see my text please how do I know the background color of an item in the recycler view at runtime and change text color accordingly if too dark make text color with an vice versa screenshot

here is some code I wrote the utile class `public class Utils {

//    ColorDrawable colorDrawable = new ColorDrawable()
private static ColorDrawable colorDrawable[] = {

        new ColorDrawable(Color.parseColor("#2e86c1")),
        new ColorDrawable(Color.parseColor("#6c3483")),
        new ColorDrawable(Color.parseColor("#239b56")),
        new ColorDrawable(Color.parseColor("#641e16")),
        new ColorDrawable(Color.parseColor("#58d68d")),
        new ColorDrawable(Color.parseColor("#7e5109")),
        new ColorDrawable(Color.parseColor("#17202a")),
        new ColorDrawable(Color.parseColor("#7d3c98")),
        new ColorDrawable(Color.parseColor("#d2b4de")),
        new ColorDrawable(Color.parseColor("#f39c12")),
        new ColorDrawable(Color.parseColor("#154360")),
        new ColorDrawable(Color.parseColor("#641e16")),
        new ColorDrawable(Color.parseColor("#145a32")),


};

public static ColorDrawable getRandomColor() {
    int r = new Random().nextInt(12);

    return colorDrawable[r];
}

} the adapter class public class NoteAdapter extends ListAdapter<Note, NoteViewHolder> {

Context context;
public static final int TYPE_ADS = 0;
public static final int TYPE_NORMAL = 1;

int clickCount = 0;

public NoteAdapter(Context context) {
    super(itemCallback);
    this.context = context;
}

static DiffUtil.ItemCallback<Note> itemCallback = new DiffUtil.ItemCallback<Note>() {
    @Override
    public boolean areItemsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
        return oldItem.getClass().equals(newItem.getClass());
    }

    @Override
    public boolean areContentsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
        return oldItem.equals(newItem);
    }
};

@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

    holder.binding.re.setBackground(Utils.getRandomColor());

    final Note note = (Note) getItem(position);

    if (note != null) {
        holder.binding.setNote(note);
    }


    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (clickCount == 3) {
                // show ads
                showAds.booleanMutableLiveData.setValue(true);
                clickCount = 0;
            } else {

                clickCount += 1;

                Intent intent = new Intent(context, NoteDetailActivity.class);
                intent.putExtra("subject", note.getSubject());
                intent.putExtra("body", note.getBody());
                intent.putExtra("date", note.getDate());
                intent.putExtra("priority", note.getPriority());
                intent.putExtra("id", note.getId());
                context.startActivity(intent);
            }
        }
    });
}   

public Note note(int position) {
    return (Note) getItem(position);
}

class ViewHolder extends RecyclerView.ViewHolder {

    NoteItem2Binding binding;

    public ViewHolder(@NonNull NoteItem2Binding binding) {
        super(binding.getRoot());

        this.binding = binding;

    }
}

} `

Eze larry
  • 36
  • 3
  • 3
    Does this answer your question? [Get background color of a Layout](https://stackoverflow.com/questions/14779259/get-background-color-of-a-layout) – Anand Jun 10 '21 at 02:43
  • If i get the color as int how do I know if its a light color or dark so I can change text color two – Eze larry Jun 11 '21 at 09:31

0 Answers0