JpCrow JpCrow - 7 months ago 46
Java Question

Android Shape Rectangle with 2 colors

I need to make a custom rectangle that should use hex colors from #bff54a up to #88c010.

Rectangle two colors

Actually I have this xml but I can only use one of those colors. How can I make the gradient possible? I already searched and I didn't found anything like this.

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

<stroke
android:width="1dp"
android:color="@color/gray_light" />

<solid android:color="#88c010" />

<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"
android:topLeftRadius="8dp"
android:topRightRadius="8dp" />

</shape>


Thanks to all!

Answer

Use Gradient as the color of the Drawable :

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

  <stroke
    android:width="1dp"
    android:color="@color/gray_light" />

  <gradient
    android:type="linear"
    android:centerX="0"
    android:centerY="1"
    android:startColor="#bff54a"
    android:endColor="#88c010" />

  <corners
    android:bottomLeftRadius="8dp"
    android:bottomRightRadius="8dp"
    android:topLeftRadius="8dp"
    android:topRightRadius="8dp" />

</shape>

See Drawable Resources