According to what I\'ve read, you can use a gradientDrawable and have three colors set for it, for example:
I think the below are possible solutions.
You can create your own GradientDrawable by extending the
GradientDrawable Class
refer to the below doc.
Gradient Drawable Documentation
put this code in your onCreate() method:
ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
new int[] {
0xFF1e5799,
0xFF207cca,
0xFF2989d8,
0xFF207cca }, //substitute the correct colors for these
new float[] {
0, 0.40f, 0.60f, 1 },
Shader.TileMode.REPEAT);
return linearGradient;
}
};
PaintDrawable paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);
and use this drawable as a background.
You can add more than three colors in xml also by creating layers. But in XML it is quite complicated.
It is not possible to do into a xml file, but you can apply +3 color gradient in yout java code with GradientDrawable class:
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);
In Kotlin you can do it like this:
replace color1,2,..n with your color values
//Create Gradient
val gradientDrawable = GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
intArrayOf(color1,color1 ,color1, colorn)
);
gradientDrawable.cornerRadius = 0f;
Using GradientDrawble we can achieve this
GradientDrawable gradientInsta = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
new int[] {
Color.parseColor("#F9ED32"),
Color.parseColor("#F6C83F"),
Color.parseColor("#F2735F"),
Color.parseColor("#EF3E73"),
Color.parseColor("#EE2A7B"),
Color.parseColor("#D22A8A"),
Color.parseColor("#8B2AB1"),
Color.parseColor("#1C2AEF"),
Color.parseColor("#002AFF”),
ContextCompat.getColor(MainActivity.this, R.color.colorPrimary)
});
findViewById(R.id.insta).setBackground(gradientInsta);