Skip to content Skip to sidebar Skip to footer

Using A Gradientdrawable With More Than Three Colors Set

According to what I've read, you can use a gradientDrawable and have three colors set for it, for example: Copy

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.

Solution 2:

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

Solution 3:

Using GradientDrawble we can achieve this

GradientDrawablegradientInsta=newGradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                newint[] {
                        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);

Solution 4:

I think the below are possible solutions.

  • You can create multiple shapes with gradients and form a bigger shape.
  • You can create your own GradientDrawable by extending the GradientDrawable Class refer to the below doc.

  • Gradient Drawable Documentation

Solution 5:

In Kotlin you can do it like this:

replace color1,2,..n with your color values

//Create Gradient valgradientDrawable= GradientDrawable(
                    GradientDrawable.Orientation.TOP_BOTTOM,
                    intArrayOf(color1,color1 ,color1, colorn)
                );
                gradientDrawable.cornerRadius = 0f;

Post a Comment for "Using A Gradientdrawable With More Than Three Colors Set"