Transparent gradient issue - android

I have layout with google map fragment in FrameLayout. Also, i placed the new view over the map with gradient background.
Layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<View
android:id="#+id/fadeTop"
android:layout_width="match_parent"
android:layout_height="#dimen/fade_height"
android:layout_gravity="top"
android:background="#drawable/list_fade_top"/>
<include layout="#layout/top_line_mid_gray"/>
</FrameLayout>
list_fade_top:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#color/fade_start_color"
android:centerColor="#color/fade_center_color"
android:endColor="#color/fade_end_color"
android:type="linear"/>
</shape>
start/end and center colors. This gradient draws with 2 light lines:
<color name="fade_start_color">#00000000</color>
<color name="fade_center_color">#4f000000</color>
<color name="fade_end_color">#ff000000</color>
start/end and center colors. This gradient draws with a 1 light line:
<color name="fade_start_color">#00000000</color>
<color name="fade_center_color">#6f000000</color>
<color name="fade_end_color">#ff000000</color>
The same behavior for gradient without center color:
<gradient
android:angle="90"
android:startColor="#color/fade_start_color"
android:endColor="#color/fade_end_color"
android:type="linear"/>
How can i draw a smooth linear gradient from black (or semi transparent black) to a full transparent?
I tried hardware acceleration on/off, 'getWindow().setFormat(PixelFormat.RGBA_8888);' Nothing helps me.
Update 1
I created a simple app at github.
https://github.com/ralexey/TestApp
Just an activity with color background and gradient over it.

add this line in your gradient android:centerY="y%p"
it may help you
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerY="25%p"
android:centerColor="#color/fade_center_color"
android:endColor="#color/fade_end_color"
android:startColor="#color/fade_start_color"
android:type="linear"
android:dither="true"
android:useLevel="true" />
</shape>

try this below code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" android:padding="10dp">
<gradient
android:startColor="#00000000"
android:centerColor="#6f000000"
android:endColor="#ff000000"
android:angle="90" />
</shape>
</item>
</layer-list>

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>

Shadow Layout with 9-patch and background color transparent?

I would like to add a custom layout an edge with shadow, exactly one like this:
http://inloop.github.io/shadow4android/
The problem is that I use this to add a semi / transparent background color: (corners_main_layout.xml)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="6dp" />
<solid android:color="#color/bright_foreground_disabled_material_dark" />
</shape>
So I can not use for background 9-patch.
Is there any way to 9-patch with shadows and transparent background color?
If not .... How could make the effect?
PS: I have seen many custom XML to add shadows, but all are very poor.
Here example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_recover_pass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="...RecoverPassActivity"
android:background="#mipmap/fondo_01"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/corners_main_layout">
</LinearLayout>
I Need shadow border rectangle White (it is transparent) similar to example 9-patch
------------EDIT---------------
I modified mi corners_main_layout.xml:
<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">
<corners android:radius="6dp" />
<solid android:color="#color/bright_foreground_disabled_material_dark" />
</shape>
</item>
<item android:drawable="#drawable/shadow_custom" />
and result :-( :
yeah! i have solution!
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="6dp" />
<solid android:color="#color/bright_foreground_disabled_material_dark" />
</shape>
</item>
<item android:drawable="#drawable/shadow_custom_9patch"/>
</layer-list>
thanks for help!

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>

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