Set android shape color programmatically

Cote Mounyo picture Cote Mounyo · Jul 24, 2013 · Viewed 126.6k times · Source

I am editing to make the question simpler, hoping that helps towards an accurate answer.

Say I have the following oval shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:angle="270"
           android:color="#FFFF0000"/>
    <stroke android:width="3dp"
            android:color="#FFAA0055"/>
</shape>

How do I set the color programmatically, from within an activity class?

Answer

Vikram picture Vikram · Jul 24, 2013

Note: Answer has been updated to cover the scenario where background is an instance of ColorDrawable. Thanks Tyler Pfaff, for pointing this out.