1

I use a selector list to pick a buttons background.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="false"
    android:drawable="@drawable/shapecopmainbutton" />
<item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbuttonpress" />
<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbutton" />
<item
    android:state_enabled="true"
    android:drawable="@drawable/shapecopmainbutton" />
</selector>

my shapecopmainbutton.xlm is as follows.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <size
                android:width="@dimen/layoutwidthMaincopbutton"
                android:height="@dimen/HeightMaincopbutton" />
            <solid android:color="@color/black"/>
        </shape>
    </item>
    <item
        android:id="@+id/gradientDrawble"
        android:left="4dp"
        android:right="1dp"
        android:top="0dp"
        android:bottom="5.5dp"
        >
        <shape
            android:id="@+id/gradientDrawbles"
            android:shape="ring"
            android:thickness="@dimen/thicknessMaincopbutton"
            android:innerRadius="@dimen/innerRadiusMaincopbutton"
            android:useLevel="false">
            <solid android:color="@color/GradientEnd" />

            <gradient
                android:id="@+id/gradientDrawbleg"
                android:type="radial"
                android:gradientRadius="30%p"
                android:startColor="@color/GradientCenter"
                android:endColor="@color/GradientEnd"
                android:centerX="0.2"
                android:centerY="0.2"
                />
        </shape>
    </item>

I want to change the start and end color of the gradient attribute of the ring shape.

this is what I have so far in my code:

EDited.

StateListDrawable stateListDrawable=(StateListDrawable) ContextCompat.getDrawable(context,R
                    .drawable.copbuttonmaineffect);
            LayerDrawable layerDrawable = (LayerDrawable) stateListDrawable.getCurrent();
            int colors[] = { R.color.GradientStartl, R.color.GradientEndl };
            GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.
                    gradientDrawble);
            gradientDrawable.setColors(colors);

I stumped here as I'm not able to figure out how to set the 'rings' start/stop color gradient attribute.

thanks.

TeleJim
  • 317
  • 3
  • 20

1 Answers1

1

You can add start and end color using setColors function of GradientDrawable.

    LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, 
        .drawable.shapecopmainbutton);
    //you can choose any color of the below. I have chosen these for purpose of example.
    //first parameter is the start color and second parameter is the end color.
    int colors[] = { 0xff255779, 0xffa6c0cd };
    GradientDrawable gradientDrawable = (GradientDrawable) 
         layerDrawable.findDrawableByLayerId(R.id.gradientDrawable);
    gradientDrawable.setColors(colors);
Anubhav Gupta
  • 2,492
  • 14
  • 27
  • I had to cast the stateDrawable to the layerDrawable before it would work. If that's how you set the gradient color, how do you set the solid color under the shape attributes? – TeleJim Aug 12 '18 at 16:35
  • This perfectly solved my problem. Thanks a bunch @Anubhav Gupta. – Rohitesh Nov 28 '18 at 04:46