Huy Nguyen
Huy Nguyen

Reputation: 1109

Android setting background color for textview that has a custom background already

I have a custom background for a TextView like that:

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

<corners android:radius="45dp"/>

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

I want to change many color of it dynamically. So I have tried to change background color by:

title.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));

But after i change background color, I can not see the corner of it. How can I change color dynamically for that textview ?

Upvotes: 0

Views: 500

Answers (1)

Marco Pierucci
Marco Pierucci

Reputation: 1215

That happens because by using title.setBackgroundColor() you are replacing your custom background hence the corners disappear.

Try retrieving the drawable instead and change its color.

GradientDrawable titleDrawable = (GradientDrawable) title.getBackground();
titleDrawable.setColor(Color.RED)

Upvotes: 1

Related Questions