0

I have a Textview with an XML file attached to the background. Now I wanted to change the background of the Textview with setBackgroundColor(), but it doesn't work, because the background color of the XML file has to be changed. How do I change the background color of the my_border XML file?

XML Textview:

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="150dp"
    android:background="@drawable/my_border"
    android:text="Hey"
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

XML Textview Border file:

<?xml version="1.0" encoding="utf-8"?>

<!-- View background color -->
<solid
    android:color="#fff" >
</solid>

<!-- View border color and width -->
<stroke
    android:width="1dp"
    android:color="#000" >
</stroke>

<!-- The radius makes the corners rounded -->
<corners
    android:radius="5dp"   >
</corners>

<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp">
</padding>

Florian
  • 25
  • 5

1 Answers1

0

try this method

private void recolor(Context context, TextView textView, @ColorInt int color,int drawableResourceId) {
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, drawableResourceId);
if (unwrappedDrawable != null) {
    DrawableCompat.wrap(unwrappedDrawable);
    DrawableCompat.setTint(unwrappedDrawable, color);
    textView.setBackground(unwrappedDrawable);
    }

}

lets say you want a red background for your TextView then this will be as following

TextView mTextView = findViewById(R.id.textView7);

recolor(this, mTextView , getResources().getColor(R.color.red),R.drawable.my_border);
Mohammed Alaa
  • 3,140
  • 2
  • 19
  • 21