Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ??

like image 529
Tapan Kumar Patro Avatar asked Mar 01 '17 10:03

Tapan Kumar Patro


People also ask

How do I create a gradient background in XML?

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.

How do I add color to a gradient in XML?

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.


2 Answers

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

like image 182
Hamza Avatar answered Oct 01 '22 18:10

Hamza


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); 
like image 32
Pelanes Avatar answered Oct 01 '22 17:10

Pelanes