How to add gradient color to color. Xml

I am using the library. In which I show the activity used by my library, I use color to cover the screen with color. he looks cool.

But for some reason, I decided to use a gradient. I designed a 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>

he looks good. But the problem is that I use a library that accepts only color or color. I have no other method for changing, the library is showing activity, I just have to pass the color that will be displayed in the action.

Now my question is:

color.xml. , , . .

, . .

1:

, , - , ,

.withColorResource(R.color.indigo)

, Color.xml, , , . , , . , .

+6
4

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

; 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"
               />

. .

0

(. )

android:background="@drawable/grad"

,

-1

, , .

, . RelativeLayout FrameLayout.

-1

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

-1
source

Source: https://habr.com/ru/post/1629528/


All Articles