How to add a gradient color in colors.xml
Asked Answered
B

5

13

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.

Berseem answered 19/2, 2016 at 8:51 Comment(7)
Regarding your "Edit 1": I don't say to use it as background for the image - I just say, set it for the image to @android:color/transparent and add a separate view, which accepts the background gradient as drawable underneath(!) it. (So the gradient has nothing to do with that lib)Ridgepole
where did I mention that I want the color on the background of the image . I just need to push the color in the method .withColorResource(R.color.indigo) , where as I want to give it a gradient colorBerseem
You are saying right in the first sentence: "In which I am showing the image and behind that image I am using a color"Ridgepole
oked ,I meant by it behind the image not as a background of the image , let me edit thisBerseem
Aha. Ok, so then it would be good to know, what library you are using for that background-color. Just curios why you need it, (and for what exactly).Ridgepole
@AllayKhalil Have you find out any solution for it?Exalted
Great question, but not sure is it possible. I am interested in this as well.Brunella
O
2

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.

Obtain answered 26/11, 2020 at 18:33 Comment(0)
D
-1

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.

Disclaimer answered 19/2, 2016 at 9:10 Comment(1)
this is not what I wantBerseem
B
-1

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

Bamboo answered 15/10, 2018 at 6:23 Comment(0)
R
-2

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.

Ridgepole answered 19/2, 2016 at 9:17 Comment(3)
I am not using the image or anything Else. I just have to give the color to the library method , I can alter thatBerseem
What library? Sounds like you use the library to load the image and set the background color. (If yes, you could use transparent background plus a second layer with a default view)Ridgepole
see my edited answer , there is a clue. I have to give a color in the method of that library and that puts the color on the background of screen , so I want to set the gradient . I do not have any other option . SO tell me how can I load the gradient file which is in drawable , how can I load the drawable as a colorBerseem
G
-3

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

Gerkman answered 19/2, 2016 at 8:59 Comment(1)
I just want the color resource I cant convert the drawable into color.xmlBerseem

© 2022 - 2024 — McMap. All rights reserved.