Thanh Duy Thanh Duy - 3 months ago 30
Android Question

How to get background of itemview in android

My code is used to show schedule from database to

RecyclerView
using
FirebaseRecyclerAdapter
. When user click into item, this item will change background, and when user click into item again, this item will change to default background.
So my question is how to get background of itemview? and how to compare with
R.color.colorPrimaryDark
.
Here is my code

mAdapter = new FirebaseRecyclerAdapter<Schedule, ScheduleViewHolder>(Schedule.class, R.layout.item_schedule,
ScheduleViewHolder.class, query) {
@Override
protected void populateViewHolder(final ScheduleViewHolder viewHolder, Schedule model, final int position) {
final DatabaseReference scheduleRef = getRef(position);
// Set click listener for the whole post view
final String scheduleKey = scheduleRef.getKey();
Log.d("ScheduleKey", scheduleKey);

viewHolder.bindToSchedule(model);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

viewHolder.itemView.setBackground(ContextCompat.getDrawable(getActivity(), R.color.colorPrimaryDark));
viewHolder.txtName.setTextColor(getResources().getColor(R.color.white));
viewHolder.txtNumber.setTextColor(getResources().getColor(R.color.white));
viewHolder.txtAddress.setTextColor(getResources().getColor(R.color.white));
viewHolder.txtIconOri.setTextColor(getResources().getColor(R.color.white));
viewHolder.txtIconHere.setTextColor(getResources().getColor(R.color.white));
}
});
}
};

Answer

your solution may be like this:

 int color = R.color.colorPrimaryDark;
        Drawable background = view.getBackground();
        if (background instanceof ColorDrawable)
            color = ((ColorDrawable) background).getColor()

in color you can get background color.