can we make multi color gradient in xml for android background? can we make multi color gradient in xml for android background? xml xml

can we make multi color gradient in xml for android background?


According to developers.androidyou 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


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);


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>