38

I have been trying to make a multi-color background in XML but there only 3 option available start, center, end and specified angles. Can't we make backgrounds like this below..multi color at different angle

multi color at different angle

Can we make like this background in android ??

Tapan Kumar Patro
  • 767
  • 1
  • 7
  • 18

9 Answers9

50

According to developers.android you can... and this is the code they used

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

<gradient
    android:angle="45"
    android:endColor="#87CEEB"
    android:centerColor="#768087"
    android:startColor="#000"
    android:type="linear" />

</shape>

also here's a tutorial

hope this helps

Hamza
  • 674
  • 4
  • 13
43

You cannot implement +3 gradient color in a xml file. But you can do it into your java/kotlin code with GradientDrawable class. This is the Java version, replace the array of colors with your colors ids.

GradientDrawable gradientDrawable = new GradientDrawable(
                Orientation.TOP_BOTTOM,
                new int[]{ContextCompat.getColor(this, R.color.color1),
                        ContextCompat.getColor(this, R.color.color2),
                        ContextCompat.getColor(this, R.color.color3),
                        ContextCompat.getColor(this, R.color.color4)});

        findViewById(R.id.background).setBackground(gradientDrawable);
Pelanes
  • 3,451
  • 1
  • 34
  • 32
  • Too late ... In my activity I have some `findelementbyid` and when I add your code to end of my code it produces error and those `findelementbyid` become red! – C.F.G Dec 15 '22 at 20:17
32

This can be achieved by using vector graphics i have made this background gradient in adobe illustrator and then import that vector asset as xml in to android studio and it works perfectly with the scalability of vectors

enter image description here

and here is the code for this vector/xml drawable

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="987.3dp"
    android:height="870.3dp"
    android:viewportWidth="987.3"
    android:viewportHeight="870.3">
    <path android:pathData="M0,870l0,-870l987,0l0,870z">
        <aapt:attr name="android:fillColor">
            <gradient
                android:endX="493.5"
                android:endY="870"
                android:startX="493.5"
                android:startY="2.6645353E-14"
                android:type="linear">
                <item
                    android:color="#FF0000FF"
                    android:offset="0" />
                <item
                    android:color="#FF6AFCFF"
                    android:offset="0.1974" />
                <item
                    android:color="#FFE900D0"
                    android:offset="0.3786" />
                <item
                    android:color="#FFFF7D15"
                    android:offset="0.5906" />
                <item
                    android:color="#FFE6FF55"
                    android:offset="0.7513" />
                <item
                    android:color="#FFED1E79"
                    android:offset="1" />
            </gradient>
        </aapt:attr>
    </path>
</vector>

To show the gradient in a horizontal orientation, use the following xml code :

enter image description here

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    android:width="96dp"
    android:height="96dp"
    android:viewportHeight="100"
    android:viewportWidth="100">
    <path android:pathData="M0,870l0,-870l987,0l0,870z">
        <aapt:attr name="android:fillColor">
            <gradient
                android:endX="99"
                android:endY="50"
                android:startX="1"
                android:startY="50"
                android:type="linear">
                <item
                    android:color="#FF0000FF"
                    android:offset="0" />
                <item
                    android:color="#FF6AFCFF"
                    android:offset="0.1974" />
                <item
                    android:color="#FFE900D0"
                    android:offset="0.3786" />
                <item
                    android:color="#FFFF7D15"
                    android:offset="0.5906" />
                <item
                    android:color="#FFE6FF55"
                    android:offset="0.7513" />
                <item
                    android:color="#FFED1E79"
                    android:offset="1" />
            </gradient>
        </aapt:attr>
    </path>
</vector>

Read more about VectorDrawable gradients here - https://blog.stylingandroid.com/vectordrawable-gradients-part1/

vepzfe
  • 4,217
  • 5
  • 26
  • 46
Harvinder Singh
  • 1,919
  • 21
  • 15
8

Create a new xml file in drawable and copy this code:

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

<gradient android:startColor="#9A0C0C"
          android:centerColor="#CE9908"
          android:endColor="#3091FF"
          android:angle="270"/>
</shape>

enter image description here

Maryam Azhdari
  • 1,161
  • 11
  • 8
2
<?xml version="1.0" encoding="utf-8"?>

<gradient
    android:endColor="#243638"
    android:centerColor="#3c6869"
    android:startColor="#7d695c"
    android:type="radial"
    android:centerX="100%"
    android:centerY="100%"
    android:gradientRadius="900dp"/>
1

Create New xml file under drawable folder and follow the code

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        
        <gradient
            android:startColor="#1fa2ff"
            android:centerColor="#12d8fa"
            android:endColor="#a6ffcb"
            android:angle="45"/>
    </shape>
</item>
In main.xml set your background to the new xml file created in drawable
Sandipan
  • 11
  • 3
1

In case anyone has this question in regards to how to achieve this kind of results with the new UI toolkit Jetpack Compose then it is fairly simple by way of using the Brush utility:

@Preview(
    device = Devices.PIXEL_4
)
@Composable
public fun GradientComposable() {
    val topRightCornerColor = Color(0xFF365d6a)
    val second = Color(0xFF126e87)
    val third = Color(0xFF769397)
    val fourth = Color(0xFF555361)
    val bottomLeftCornerColor = Color(0xFF4f4858)
    val angleBrush = Brush.linearGradient(
        colors = listOf(
            topRightCornerColor,
            second,
            third,
            fourth,
            bottomLeftCornerColor
        ), start = Offset(
            x = Float.POSITIVE_INFINITY,
            y = 0f
        ), end = Offset(
            x = 0f,
            y = Float.POSITIVE_INFINITY
        )
    )
    Box(
        modifier = Modifier
            .background(brush = angleBrush)
            .fillMaxSize()
    )
}

This creates a linear gradient from the top right most corner down to the left most corner (the offset sets the angle and direction, up or down).

The more colors are supplied at intervals between the first and last colours, the closer to the correct gradient you will get. Here I only use 5 colors.

The resulting background being as follows:

enter image description here

Hrafn
  • 2,867
  • 3
  • 25
  • 44
0

You can do it with layer-list of radial gradients, then you set different opacity for different items

Pete
  • 19
0

In Kotlin you can just use this extension function, code is taken by @Pelanes

fun ImageView.setGradientDrawable(colorArray: IntArray) {
val gradientDrawable = GradientDrawable(
    GradientDrawable.Orientation.TOP_BOTTOM, colorArray
)
this.background = gradientDrawable}
Ahmet B.
  • 1,290
  • 10
  • 20