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.

Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
Allay Khalil
  • 674
  • 3
  • 11
  • 31
  • 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) – everyman Feb 19 '16 at 10:51
  • 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 color – Allay Khalil Feb 19 '16 at 10:52
  • You are saying right in the first sentence: "In which I am showing the image and behind that image I am using a color" – everyman Feb 19 '16 at 10:54
  • oked ,I meant by it behind the image not as a background of the image , let me edit this – Allay Khalil Feb 19 '16 at 10:58
  • 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). – everyman Feb 19 '16 at 11:02
  • @AllayKhalil Have you find out any solution for it? – SARATH V Jun 11 '19 at 06:26
  • Great question, but not sure is it possible. I am interested in this as well. – I.Step Feb 11 '21 at 19:04

5 Answers5

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.

woodenleg
  • 84
  • 1
  • 9
-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.

caliskan
  • 107
  • 1
  • 6
-1

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

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

everyman
  • 3,377
  • 1
  • 34
  • 33
  • I am not using the image or anything Else. I just have to give the color to the library method , I can alter that – Allay Khalil Feb 19 '16 at 10:02
  • 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) – everyman Feb 19 '16 at 10:43
  • 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 color – Allay Khalil Feb 19 '16 at 10:48
-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

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142