I have a TextView

 <TextView android:id="@+id/tv_rowMainFragment_pushBadge" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="20dp" android:layout_marginRight="10dp" android:background="@drawable/shape_badge" android:gravity="center" android:lines="1" android:paddingLeft="10dp" android:paddingRight="10dp"/> 

I pass drawable

 android:background="@drawable/shape_badge" <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="@color/color_white"/> </shape> 

Now my TextView round and has a white background color.

Now the question is how to dynamically change the background color and at the same time leave a round shape?

    1 answer 1

    I do this:

     Drawable shapeDrawable = getResources().getDrawable(R.drawable.my_back); //Или Drawable shapeDrawable = myImg.getBackground(); shapeDrawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY); myImg.setBackgroundDrawable(shapeDrawable); 

    If you do with the alpha channel, it is desirable on a white (necessary) background, since the overlay occurs.

    • Yes, the only problem is that TextView no getBackgrondDrawable() method - Aleksey Timoshchenko
    • And just getBackground(); does not work? - Jarvis_J
    • So it works)) ok - Aleksey Timoshchenko