Android change my custom button color in java
<p>I have a custom button defined in xml. I want to change it so that the stroke color and gradient colors can be changed at run time.</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    &lt;item android:state_pressed="true"&gt;
        &lt;shape&gt;
            &lt;gradient android:startColor="#330000"
                android:endColor="#000000" android:angle="270" /&gt;
            &lt;stroke android:width="1dp" android:color="#aa0000" /&gt;
            &lt;corners android:radius="8dp" /&gt;
            &lt;padding android:left="2dp" android:top="2dp"
                android:right="2dp" android:bottom="2dp" /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item android:state_focused="true"&gt;
        &lt;shape&gt;
            &lt;gradient android:endColor="#330000"
                android:startColor="#000000" android:angle="270" /&gt;
            &lt;stroke android:width="1dp" android:color="#aa0000" /&gt;
            &lt;corners android:radius="8dp" /&gt;
            &lt;padding android:left="2dp" android:top="2dp"
                android:right="2dp" android:bottom="2dp" /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;shape&gt;
            &lt;gradient android:endColor="#000000"
                android:startColor="#000000" android:angle="270" /&gt;
            &lt;stroke android:width="1dp" android:color="#aa0000" /&gt;
            &lt;corners android:radius="8dp" /&gt;
            &lt;padding android:left="2dp" android:top="2dp"
                android:right="2dp" android:bottom="2dp" /&gt;
        &lt;/shape&gt;
    &lt;/item&gt;

&lt;/selector&gt;
</code></pre>

<p>I know how to get the Drawable with</p>

<pre><code>Drawable d = getResources().getDrawable(R.drawable.custom_button);
</code></pre>

<p>and then something like</p>

<pre><code> btnSettings.setBackgroundDrawable(d);
</code></pre>

<p>What I can't figure out is how to get the states and then the shape for each state. And then ultimately, modify the shape attributes such as stroke color and gradient colors.</p>

<p>Edit: After researching this for a bit, it looks like I will need to pretty much replace all of this xml code with java. I would like to rephrase my question such that I need to know how to create the selector, complete with three states and different shape characteristics for each state. The answer does not need to show all three states. Once I see how to do one, I can figure out the rest.</p>
