3

Currently I have 3 drawable XML files defining 3 separate gradients. these gradients are dynamically set as the background color of an imageView in my code (which is working fine).

example: drawable\morningsky.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:bottom="4dp">
  <shape>
     <gradient
        android:startColor="@color/blue"
        android:endColor="@color/dark_blue"
        android:angle="270" />
  </shape>

example: drawable\eveningsky.xml

<?xml version="1.0" encoding="utf-8"?>
<item android:bottom="4dp">
  <shape>
     <gradient
        android:startColor="@color/orange"
        android:endColor="@color/yellow"
        android:angle="270" />
  </shape>

I am setting the backgrounds in my imageView this way:

iv.setBackgroundResource(R.drawable.morningsky);

All is good, but do I really need to use multiple different drawable resource files for each gradient? Is there any way I can define all gradients in one single drawable file and then load that gradient from my code?

Bluemarble
  • 1,925
  • 4
  • 20
  • 35
  • see this question: http://stackoverflow.com/questions/5702143/multipe-shapes-inside-shapes-xml-in-android – samgak Mar 25 '15 at 07:24
  • Hi Samgak, I got the part of adding multiple tags in one xml, but what I need to know is how do I refer the particular from my code? – Bluemarble Mar 25 '15 at 07:28
  • 1
    iv.getBackground().setLevel(1); set to the correct index (not resource id). you need to have set the level list as the background already – samgak Mar 25 '15 at 08:19

3 Answers3

7

you can use something like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:type="radial"
                android:gradientRadius="300"
                android:startColor="@android:color/white"
                android:endColor="@android:color/transparent"
                android:centerX="0.25"
                android:centerY="0.5"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:type="radial"
                android:gradientRadius="300"
                android:startColor="@android:color/white"
                android:endColor="@android:color/transparent"
                android:centerX="0.75"
                android:centerY="0.5"/>
        </shape>
    </item>
</layer-list>
Juis Kel
  • 361
  • 4
  • 8
  • I needed 2 gradients, one from every direction - this worked like a charm for me. – arenaq Apr 08 '19 at 15:29
  • 1
    Top Tip: If you have a background, make sure to only set the first item `android:endColor="@color/my_color"` (which is your background/fill color) and the subsequent items `android:endColor="@color/transparent_100"` otherwise the following items will cover the previous items completely – Pierre Oct 21 '19 at 08:13
1

You can do it programatically :

public void setShadeBackground(View v){
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                    new int[] {
                            0xFF2583ED,
                            0xFF8220C9,
                            0xFFBC0BCC,
                            0xFFD625ED ,
                            0xFFD407AE ,
                            0xFFF2115C }, //substitute the correct colors for these
                    new float[] {
                            0, 0.30f,0.50f, 0.60f,0.70f, 1 },
                    Shader.TileMode.REPEAT);
            return linearGradient;
        }
    };
    PaintDrawable paint = new PaintDrawable();
    paint.setShape(new RectShape());
    paint.setShaderFactory(shaderFactory);
    v.setBackgroundDrawable((Drawable)paint);

}
Shrini Jaiswal
  • 1,090
  • 12
  • 12
0

You can use the selector

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true" android:drawable="@drawable\eveningsky.xml"/>
      <item android:state_selected="true" android:drawable="@drawable\morningsky.xml"/>
      <item android:drawable="@drawable\morningsky.xml"/>
   </selector>
Dmitriy Puchkov
  • 1,530
  • 17
  • 41