1

This is for api < 21

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">

    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

        <size
            android:width="60dp"
            android:height="60dp" />

        <solid android:color="#007fa9" />

    </shape>
</item>

<item android:id="@+id/shape_id" >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

        <size
            android:width="60dp"
            android:height="60dp"
            />

        <solid android:color="#007fa9" />
    </shape>
</item>

</selector>

As you see I have a selector with states inside and also shapes, How can I change the solid color of the shape programatically? I'm setting this as the background of a Relative Layout, so it behaves like a button changing its color when it's pressed, or maybe there's a way to create a shape programatically, if so how?

For api 21 and above

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#007fa9">

<item android:id="@android:id/mask">

    <shape android:shape="oval">

        <solid android:color="#007fa9" />

    </shape>

</item>

<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">

        <size
            android:width="60dp"
            android:height="60dp" />

        <solid android:color="#007fa9" />

    </shape>

</item>

</ripple>

How do you do it for this one?

Ray
  • 29
  • 1
  • 7

1 Answers1

0

try this:

Give an id to your Drawable selector:

<item  android:id="@+id/shape_id">

Then in Code:

LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(YourActivity.this,R.drawable.shape_id); //get the ID
shape.setColor(Color.Black); // change color
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62