Gradient shape blending position in LinearLayout - android

I have a requirement of achieving following type of gradient on my LinearLayout
Can someone help me to achieve this?

Try this:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffff"
android:centerColor="#ffffff"
android:endColor="#ff0000"
android:angle="270"
/>
</shape>

defining in xml file you can get like this way
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffffff"
android:endColor="#ffff0000"
android:angle="270"
/>
<corners android:radius="1dp"/>
</shape>
save this in mygradientbg.xml into drawable folder
in you linearlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/gradient"
>
</LinearLayout>
but it will stretch with the width and height

Related

adding gradient colour to button background in 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>

Gravity right circle drawable by using xml

I am trying to achieve drawable to looks like this:
I use this xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:gravity="end">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:width="#dimen/bullet_size"
android:height="#dimen/bullet_size"
/>
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
</layer-list>
But got this:
Any ideas what could be wrong?
I am using the gravity right circle dralable as follows:
textView.setBackgroundResources(R.drawable.right_shspe);
I think you should center the item which is holding the oval shape vertical and everything will be OK.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:gravity="end|center_vertical">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
>
<size
android:width="#dimen/bullet_size"
android:height="#dimen/bullet_size"
/>
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
</layer-list>
Change The Width and height accordingly and it will fix the shape

How to customize GridView vertical scrollbar?

I tried changing scrollbar of GridView:
android:scrollbarThumbVertical="#drawable/scrollbar_vertical_thumb"
android:scrollbarTrackVertical="#drawable/scrollbar_vertical_track"
scrollbar_vertical_thumb.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="0"
android:endColor="#005A87"
android:startColor="#007AB8" />
<corners android:radius="6dp" />
</shape>
scrollbar_vertical_track.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="0"
android:endColor="#9BA3C5"
android:startColor="#8388A4" />
<corners android:radius="6dp" />
</shape>
But it's not working and gridview keeps showing original scrollbar.
please try
android:scrollbarAlwaysDrawVerticalTrack=true
android:scrollbars=vertical
if it didnt work then try android:scrollbarSize too

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

Can't get ListView background gradient to show

I have a listview and I want to have a background gradient that covers the entire listview. I don't want a gradient for each row. All I get is a white background.
Code for gradient in mybg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#e6e6e6"
android:endColor="#ffffff"
android:angle="45" />
</shape>
</item>
</selector>
My ListView:
<ListView
android:id="#+id/lvEvents"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/mybg"
android:cacheColorHint="#00000000"
android:divider="#ffc0c0c0"
android:dividerHeight="1dp"
android:scrollingCache="true" />
Try set the row view background to be transparent by using #null.
You missed the shape , try this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:centerColor="#000000"
android:endColor="#404040"
android:startColor="#404040" />
</shape>

Categories

Resources