I am facing issue change color or drawable resource programmatically from the recycler adapter holder
for API 21.
In the below code, I am changing the drawable resource for blow API 23 but nothing is happening. I have tried to change the drawable background color neither that work.
I would prefer to change only background tint if possible for API 21 so I can avoid using extra drawable but if not possible than I can use one.
Note: For default style, I have set TextView background (drawable) in <style>
.
@Override
public void onBindViewHolder(@NonNull StepHolder holder, int position) {
Step step = mStep.get(position);
holder.setCount(step.getCount());
holder.setDescription(step.getDescription());
if (position <= 2) {
if (Build.VERSION.SDK_INT < 23) {
// change drawable color property
holder.count.setBackgroundResource(R.drawable.shape_circle_accent);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
}
}
public class StepHolder extends RecyclerView.ViewHolder {
public TextView count, description;
public View timeline;
public StepHolder(@NonNull View itemView) {
super(itemView);
this.count = itemView.findViewById(R.id.v_step_count);
this.description = itemView.findViewById(R.id.v_step_description);
}
public void setCount(String count) {
this.count.setText(count);
}
public void setDescription(String description) {
this.description.setText(description);
}
public void setTimeline(View timeline) {
this.timeline = timeline;
}
}
public class Step {
private String count, description;
public Step(String count, String description) {
this.count = count;
this.description = description;
}
public String getCount() {
return count;
}
public String getDescription() {
return description;
}
@Override
public String toString() {
return "Step{" +
"count='" + count + '\'' +
", description='" + description + '\'' +
'}';
}
}
<TextView
android:id="@+id/v_step_count"
style="@style/StepsCounterTheme"
android:layout_marginStart="@dimen/app_space"
android:layout_marginEnd="@dimen/app_space"
android:text="@string/_1"
app:layout_constraintBottom_toTopOf="@+id/v_step_timeline_path"
app:layout_constraintEnd_toStartOf="@+id/v_step_description"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/colorWhite" />
</shape>
Here I have realized the issue is with the style. Means if I set the
backgroundTint
in style, the code for API < 23 won't affect. Once I have tried removingbackgroundTint
from style. The code is working.
<!-- steps to prepare timeline counter theme -->
<style name="StepsCounterTheme" parent="TextAppearance.MaterialComponents.Body1">
<item name="android:layout_width">@dimen/app_space_ot</item>
<item name="android:layout_height">@dimen/app_space_ot</item>
<item name="android:background">@drawable/shape_circle</item>
<item name="android:backgroundTint">@color/colorPrimary</item> <!-- this one -->
<item name="android:textColor">@color/colorWhite</item>
<item name="android:gravity">center</item>
</style>
Question
So now I wonder how to set the default
backgroundTint
in style? Do I have to create version specific style? Will that allow overriding in Java for API < 23?
Try this
holder.count.setBackground(mContext.getResources().getDrawable(R.drawable.shape_circle_accent));
holder.count.setBackgroundTintList(mContext.getResources().getColorStateList(R.color.colorD));
Try this,
private void setBackgroundColor (TextView tv){
GradientDrawable bgShape = (GradientDrawable) tv.getBackground();
bgShape.mutate();
bgShape.setColor(ContextCompat.getColor(mContext.getApplicationContext(), R.color.colorAccent));
}
Usage
if (Build.VERSION.SDK_INT < 23) {
setBackgroundColor(holder.count);
} else {
holder.count.setBackgroundTintList(mContext.getColorStateList(R.color.colorAccent));
}
I don't know if I understood the question well. haha..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.