Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the gradient color in color . Xml

I am using a library . In which I am showing the activity used by my library I am using a color to cover up the screen with color. it is looking cool.

But due to some reasons I have decided to use gradient. I have developed the beautiful gradient effect as you can see below

<?xml version="1.0" encoding="UTF-8"?>

    <gradient
        android:angle="90"
        android:endColor="#555994"
        android:centerColor="#b5b6d2"

        android:startColor="#555994"
        android:type="linear" />

    <corners
        android:radius="0dp"/>

</shape>

it is looking good. But problem is I am using library which only accepts the color or color resource. I have no other method to alter , the library is showing the activity it self , I just need to pass the color that will appear on the activity.

Now My question is :

Can I define directly the gradient color in the color.xml file. Or is there any way to convert the gradient color file which is in draw able , can be pick as a color. so that , the library can apply my custom gradient color as a back ground

please help me . It would be a grate help .

Edit 1:

For those guys who are saying me to add this as a background of the image or any thing else , let me share you a little part of that line so that you can understand the case more clearly

.withColorResource(R.color.indigo)

Here I am referencing to the purple color defined in the Color.xml, I can set any thing as a background as library only gives me option of setting color as shown above. So I have made a draw able of gradient color , Now I want that how to reference the gradient as a color.

like image 503
Allay Khalil Avatar asked Feb 19 '16 08:02

Allay Khalil


People also ask

How do you color 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.

How do you add color to a gradient?

Select the Gradient tool in the toolbar. In the selected artwork you'll see the gradient annotator, which shows the gradient slider and the color stops. Double-click a color stop on the artwork to edit the color, drag the color stops, click beneath the gradient slider to add new color stops, and more.

How do I create a gradient background in XML?

Inside the XAML Grid element, place a Grid. Background element. Inside that place a gradient brush element. This example uses a LinearGradientBrush that shades colors from the control's upper left corner (0, 0) to its lower right corner (1, 1).


1 Answers

Colors in Android are represented as Int (and I don't mean that as R.color.white is Int - that's just id of the color resource) which is just number representation of normal hex color, e.g. #f00 -> 0xFFFF0000. That means that there is no way to represent gradient as single color resource.

TLDR: gradient is not color but set of colors with angle.

like image 71
woodenleg Avatar answered Sep 28 '22 05:09

woodenleg