How to add a gradient color in colors.xml

19.3k views Asked by At

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.

5

There are 5 answers

0
woodenleg On

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.

1
Amit Vaghela On

You just need to create a drawable resource (see an example below), and add it to the layout you created

android:background="@drawable/grad"

Please check gradient background answer as well as similar gradient effect

1
caliskan On

example for button. That is drawable resource, my_button_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient
                android:angle="180"
                android:centerColor="@color/flat_silver_sand"
                android:endColor="@color/flat_white_smoke"
                android:startColor="@color/flat_white_smoke" />
        </shape>
    </item>
</layer-list>

if you want to use selector; btnselector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/transparent_black_10" android:state_focused="true" android:state_pressed="false" />
    <item android:drawable="@drawable/my_button_gradient" android:state_focused="true" android:state_pressed="true" />
    <item android:drawable="@drawable/my_button_gradient" android:state_focused="false" android:state_pressed="true" />
</selector>

layout.xml;

...

 <Button
 android:id="@+id/btn"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentEnd="true"
 android:layout_alignParentRight="true"
 android:layout_alignParentTop="true"    
 android:background="@drawable/btnselector" 
 android:textColor="#34495E"
               />

You can write code like this for your image. That maybe help you.

3
everyman On

But problem is I am using library which only accepts the color or color resource.

As the others stated, create a resource from your gradient and use it behind the image with a transparent background color. Stack the views with RelativeLayout or FrameLayout.

0
Shinde Ajay On

you can use btnSubmit.setBackgroundResource(R.drawable.button_gradient) in your onCreate() method.