adding gradient colour to button background in android - android

I have a gradient background with code:
<Button android:id="#+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login"
android:background="#drawable/sel_btn_background"
android:gravity="center"
android:textSize="16sp"
android:layout_marginTop="40dp"
android:textColor="#android:color/white"/>
sel_btn_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#color/bluegreen"
android:endColor="#color/aqua_marine"
android:angle="-270" />
</shape>
I get result:
How to achieve the below result(left side is the light colour fading to dark on right colour) What value should be added for angle:

Use 180 angle instead of -270 angle
android:angle="360"

It should be 360,Try the following:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#36d1d1"
android:centerColor="#811010"
android:endColor="#233308"
android:angle="360" />
</shape>
Replace your color values accordingly.I have used completely different color distinguishing between all 3 colors.

try this...
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#278b79"
android:centerColor="#32b39b"
android:endColor="#38c7ad"
android:angle="180" />
</shape>

Related

I want to add strokes to a view

I'm trying to add strokes to a view using drawable.
But I want add it only in side parts(left and right), not in whole parts.
Is there any good way to add stroke in side parts?
Below is my drawable xml code. (leanear_border_gray.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
android:color="#778899"/>
</shape>
and below is my target view xml code.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/linear_border_gray"
android:layout_weight="1">
....
</LinearLayout>
You need to wrap your shape into layer-list drawable with rectangular shape, add stroke and using top and bottom margins with negative values "hide" top and bottom lines, like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-4dp" android:bottom="-4dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#778899"/>
</shape>
</item>
</layer-list>
You can do it by using layer-list drawable suggested by #iDemigod
Your Layout should be like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="#drawable/border_left_right"
android:layout_gravity="center"
android:layout_weight="1">
</LinearLayout>
Note: If you are giving android:layout_height="match_parent" and android:layout_weight="1" then you must have to set android:layout_width="0dp" and vice versa for android:layout_width="match_parent".
File: border_left_right.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-4dp" android:bottom="-4dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#07060b"/>
</shape>
</item>

Multi color Stroke image with XML

Does it possible to create multi-color stroke with the help of XML? I want to create exact view attached here.
Thanks
You could use a trick by creating a drawable shape with gradient and make it a parent's background... something like this:
gradient_stroke.xml (in res/drawable):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="180"
android:startColor="#fff96200"
android:centerColor="#fffff60f"
android:endColor="#fff96200"
android:centerX="50%"
>
</gradient>
<corners android:radius="5dp"></corners>
</shape>
</item>
your_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:padding="2dp"
android:background="#drawable/gradient_stroke"
android:layout_height="wrap_content">
<Button android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/green"
android:id="#+id/button"/>
</LinearLayout>
Just an idea - play around with it to get desired values...

android XML change gradient color

i need to change android:color in XML file in run-time
i use it for an image button source
<ImageButton
android:layout_width="#dimen/large_brush"
android:layout_height="#dimen/large_brush"
android:background="#android:color/transparent"
android:contentDescription="#string/paint"
android:src="#drawable/paint"/>
my XML paint file looks like this
<?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="oval" >
<stroke
android:width="1dp"
android:color="#android:color/holo_purple" />
<gradient
android:startColor="#android:color/holo_purple"
android:endColor="#android:color/holo_purple" />
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
i tried DOM parser but after getting to gradient it shows that there are no more nodes
Try like this way. Hope it should helpful for you. Thanks
Create a new xml file and put it in drawable and then add it to button as background
gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Gradient Bg for listrow -->
<gradient
android:startColor="#f1f1f2"
android:centerColor="#e7e7e8"
android:endColor="#cfcfcf"
android:angle="270" />
</shape>
layout.xml
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/gradient"
android:text="Button Text" >
You have the background set to transparent, the button will never have a background. Set it to you XML drawabale resource instead.

View with Solid Background and Top+Bottom Inner Shadows

Essentially, I am trying to create the following background:
The traditional gradient which use in the drawable that I use for background only supports start color, middle color and end color.
However, as you can see from the mockup, I am trying to create only a slight overlay/shadow at the top and bottom of the shape, with a #50000000 color (black with 50% opacity).
If you're using this inside a Layout view, then you can simply create a View with a gradient background and place it in the beginning and in the end of the Layout.
For example:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/parent">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#drawable/gradient" />
<!-- Your other child views -->
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#drawable/gradient" />
</LinearLayout>
And your gradient.xml file will have this:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#FFFFFF" android:endColor="#000000" android:angle="90"/>
</shape>
You can specify the blue background color to the parent layout.
You'll essentially get something like this:
[EDIT]
You can create two drawables - gradient_top.xml and gradient_bottom.xml to get the angle right
I prefer doing this than having to mess around with 9-Patches. Although, having said that, I wish Google would get on with providing built-in support for drop shadows, since they're so common.
Just to build on JoelFernandez solution with a more complete example:
The Container:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="[your_container_height]"
android:background="#drawable/container_bg_color">
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentTop="true"
android:background="#drawable/container_gradient_top"/>
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentBottom="true"
android:background="#drawable/container_gradient_bottom" />
<!-- Insert your content here -->
</RelativeLayout>
The Background Color (container_bg_color.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient android:startColor="#4a6fb4"
android:endColor="#color/deepBlue"
android:angle="135"/>
</shape>
The Top Gradient (container_gradient_top.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient
android:startColor="#00222222"
android:centerColor="#11111111"
android:endColor="#44000000"
android:angle="90"/>
</shape>
The Bottom Gradient (container_gradient_bottom.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<gradient
android:startColor="#44000000"
android:centerColor="#11111111"
android:endColor="#00222222"
android:angle="90"/>
</shape>
Result:
Elaborating #ramaral's answer, build this drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF408DAA"/>
</shape>
</item>
<item android:top="0dip" android:bottom="32dip">
<shape android:shape="rectangle">
<gradient android:startColor="#00000000" android:endColor="#50000000" android:angle="90"/>
</shape>
</item>
<item android:top="32dip" android:bottom="0dip">
<shape android:shape="rectangle">
<gradient android:startColor="#50000000" android:endColor="#00000000" android:angle="90"/>
</shape>
</item>
</layer-list>
You would need to set a fixed height in the view, in order to achieve the best result. In my case I was setting the height to "36dip". Notice that the "32dip" is the amount of space from where the gradient ends to the end of the drawable, so that would leave me with a top and bottom gradients of "4dip" (36-32=4 :p)
Start with this:
Create a drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<solid android:color="#408DAA"/>
</shape>
</item>
<item >
<shape android:shape="rectangle">
<solid android:color="#50000000"/>
</shape>
</item>
<item android:top="10dip" android:bottom="10dip">
<shape android:shape="rectangle">
<solid android:color="#408DAA"/>
</shape>
</item>
</layer-list>
Use it as background of any view.
Adjust top, bottom and color="#408DAA" according your needs

Android Gradient does not fill background

I'm in trouble with gradient background. Result is not what I want. I cannot change anything on gradient.
shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#color/holo_orange_dark"
android:startColor="#color/holo_green_dark" />
</shape>
color.xml
<color name="holo_green_dark">#ff669900</color>
<color name="holo_orange_dark">#ffff8800</color>
item.xml
<View
android:layout_width="fill_parent"
android:layout_height="10dp"
android:layout_alignBottom="#+id/term"
android:layout_alignLeft="#+id/term"
android:background="#drawable/shadow"
/>
It is always black and white for every color. What is the problem ?
Change your view background from android:background="#drawable/shadow" to android:background="#drawable/gradient" as you mentioned as gradient.xml
Use following code to apply gradient for a view or layout.
activity_main.xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#drawable/gradient_transparent"/>
gradient_transparent.xml(res/drawable) :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#000000"
android:centerColor="#7f000000"
android:endColor="#0f000000"
android:angle="90"
android:dither="true"
/>
</shape>

Categories

Resources