1

I have the following

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >


    <corners android:radius="20dp"/>
</shape>

Which I set as a background to my layout.

I can set the color using the following

root.getBackground().setColorFilter(Color.parseColor("#ab2233"), PorterDuff.Mode.SRC_ATOP);

However this sets a solid color. I would like to set a gradient color instead and then pass this color around for other backgrounds. Is there a way I can set a gradient? I know there is a solution on SO using canvas but I was hoping to avoid it especially that I want to keep the rounded corners and any other shape features.

Is there a way I can set Color gradient dynamically? I thought about introducing a gradient in a shape but I need the color to be dynamic.

Any solutions?

Thanks

Edit: This is not a duplicate as I specifically asked to keep using the xml but just tweak the color value

SOFe
  • 7,867
  • 4
  • 33
  • 61
Snake
  • 14,228
  • 27
  • 117
  • 250
  • 1
    I think instead of using the xml to set radius you can do that also programmatically as suggested by yvette.. – sanjeev Aug 12 '19 at 06:31

2 Answers2

0

create background.xml in /res/drawable:

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFFFF"
        android:endColor="#00000000"
        android:angle="45"/>    
</shape>

and in your layout file in /res/layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >   
</LinearLayout>

you can use other features like radius , padding ,etc.

and programmatically it would br like as below

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);
Mike
  • 58
  • 8
  • 1
    The java code is a replacement for the xml. I want to keep my xml and just change color in java . – Snake Aug 12 '19 at 06:13
0

Add this in xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:endColor="@color/colorPrimary"
        android:startColor="@color/white" />

    <corners android:radius="20dp"/>
</shape>

or add this in .java file

 View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
  • 1
    The xml one is setting predetermined colors. I need to set them dynamically at run time as color is random. The java solution completely replaces the xml. I was hoping to keep the xml and just tweak the color portion of things as per my question – Snake Aug 12 '19 at 06:12