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..
Can we make like this background in android ??
GradientStop Class: Describes the location and color of a transition point in a gradient. This example shows how to use the LinearGradientBrush class to paint an area with a linear gradient. In the following example, the Background of a Border is painted with linear gradient that transitions from yellow to white.
To create a gradient color we need to create a . xml file in the drawable folder. So go to app -> res -> drawable and right-click on drawable -> New -> Drawable Resource File and create gradient_drawable. xml file.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With