Semi transparent image background in Android shape xml - android

I want to make a rectange object (shape) with gradient and below it image background. I've got:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="#BF0e2634"
android:endColor="#BF0e2634"
android:angle="270"/>
<stroke
android:width="1dp"
android:color="#0e2634" >
</stroke>
<size
android:width="15dp"
android:height="15dp"/>
<corners android:topLeftRadius="3dp" android:topRightRadius="3dp"
android:bottomLeftRadius="3dp" android:bottomRightRadius="3dp"/>
</shape></item>
<item
android:drawable="#drawable/galaxy"
android:alpha="0.2"
/>
</layer-list>
But the image is overide the rectange with stroke and gradient. Please help me guys!

Related

Bottom Rounded Corners - Android

I have been trying to fix this but for whatever I do, I just cannot get the Bottom Rounded Corners the way I want, I am using the following
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<stroke android:width="0dp" />
<solid android:color="?attr/colorPrimaryDark" />
</shape>
</item>
<item android:top="0dp" android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="0dp"/>
<solid android:color="?attr/colorPrimary"/>
<corners android:radius="15dp" android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="0dp" android:topRightRadius="0dp"/>
</shape>
</item>
</layer-list>
The Results are the following
It creates the corners but it adds grey color (colorPrimaryDark) to it where I want it to be white (colorPrimary") with a grey line - Any idea how I accomplish this?
A little hack is possible to get rid of line on top and sides: set negative offsets.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-1dp"
android:right="-1dp"
android:top="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="?attr/colorPrimaryDark" />
<solid android:color="?attr/colorPrimary" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:radius="15dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
Not sure will it work correctly on all devices / Android versions or not.
If you only want bottom border a Shape drawable will be enough . You do not need a layer-list for it .
try this
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="#color/green"/>
<solid android:color="?attr/colorPrimary" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp" />
</shape>
The problem with your current code is first item have a solid color set . this is why the gray part showing .

How can I recreate this background in xml?

I've been trying to recreate a background image in xml (as drawable). Since the background is a simple shape, it would be better if it is created as xml drawable.
The is a really large gradient circle that squares off at the left, top and right border.
What I've tried
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<size
android:height="20dp" />
<gradient
android:type="linear"
android:startColor="#color/gradientLeft"
android:centerColor="#color/gradientMiddle"
android:endColor="#color/gradientRight"
android:angle="0" />
</shape>
</item>
<item>
<shape
android:shape="oval">
<padding
android:top="20dp" />
<gradient
android:type="linear"
android:startColor="#color/gradientLeft"
android:centerColor="#color/gradientMiddle"
android:endColor="#color/gradientRight"
android:angle="0" />
</shape>
</item>
</layer-list>
I've tried to create a layer-list. This layer list contains 2 shapes, a rectangle and an oval.
The rectangle has a gradient and a height of 20dp (to test).
The oval also has the same gradient, and has a top padding of 20dp.
This however, doesn't create a circular shape, but fills the whole shape with the gradient. Can someone help me?
Try this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" />
</item>
<item
android:bottom="300dp"
android:left="-100dp"
android:right="-100dp"
android:top="-80dp">
<shape android:shape="oval">
<size android:height="20dp" />
<gradient
android:angle="0"
android:centerColor="#color/colorAccent"
android:endColor="#color/red"
android:startColor="#color/colorPrimary"
android:type="linear" />
</shape>
</item>
</layer-list>
OUTPUT
Try this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item>
<shape android:shape="rectangle" >
<solid android:color="#ffffff"/>
</shape>
</item> <item
android:bottom="400dp"
android:left="-200dp"
android:right="-200dp"
android:top="-100dp">
<shape android:shape="oval">
<size android:height="20dp" />
<gradient
android:type="linear"
android:startColor="#FD4F33"
android:centerColor="#EE3A53"
android:endColor="#DF2772"
android:angle="0" />
</shape> </item> </layer-list>
The background will be like this

How to center the gradient color in android

Hi I am trying to gradient in android.
I am trying the following code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:centerColor="#FFEBEE"
android:endColor="#EF5350"
android:angle="-270" />
</shape>
Result:
I want to increase the size of white in center. Please help me to solve my issue.
Try this :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="50dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:endColor="#EF5350"
android:centerColor="#FFFFFF"
android:angle="270"/>
<size
android:height="100dp"
android:width="80dp"/>
</shape>
</item>
<item
android:bottom="50dp">
<shape android:shape="rectangle">
<gradient
android:startColor="#EF5350"
android:endColor="#FFFFFF"
android:centerColor="#FFFFFF"
android:angle="270"
/>
<size
android:height="100dp"
android:width="80dp"/>
</shape>
</item>
</layer-list>
You need to specify type inside the gradient tag,
For e.g. in my case I have defined radial for center gradient effect,
android:type="radial"
I have created gradient shape (centergradietbg.xml) with center gradient effect, look it my code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:centerColor="#c1c1c1"
android:endColor="#4f4f4f"
android:gradientRadius="400"
android:startColor="#c1c1c1"
android:type="radial" >
</gradient>
</shape>
Use this xml in your layout background using
android:background="#drawable/centergradietbg"

Android : How to create oval size drawable resouce?

I am trying to create drawable resource oval com rectangle shape? I want exact the below shape
But I am getting the follow :
what I'm using:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:startColor="#36D53D"
android:centerColor="#36D53D"
android:endColor="#36D53D"
android:angle="90"/>
<padding android:left="3dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
<corners android:radius="160dp"></corners>
</shape>
how to create the shape what I want? Thank you in advance
Try this one for oval.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<!-- fill/color -->
<solid
android:color="#ff0000"/>
<!-- Control the width and height of the shape -->
<size
android:width="120dp"
android:height="70dp"/>
</shape>
Rectangle with rounded corner
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- fill/color -->
<solid
android:color="#ff0000"/>
<!-- Control the width and height of the shape -->
<size
android:width="200dp"
android:height="70dp"/>
<!-- Control the radius of each corners -->
<corners
android:topLeftRadius="30dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"/>
</shape>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#189B5F" />
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
/>
</shape>
use shape drawable and for android:shape give rectangle
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
</shape>
increase the corner dp value can keep checking. i think giving around 20-25dp for all corners will give you that shape. And for correction it is not oval it is rounded rectangle.

Applying background image to a shape

I dont know whether I am asking is wrong or not.
But can I have an image in this shape xml so that if provide an image then it's corners become rounded.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#F87217"/>
<stroke
android:width="1dip"
android:color="#f0f0f0" />
</shape>
You may try this
<item
android:drawable="#drawable/ic_broadcast"
android:left="3px"
android:right="3px"
android:top="3px">
<shape android:shape="rectangle" >
<solid android:color="#cc2b2b" />
<corners android:radius="8dp" />
</shape>
</item>
ic_broadcast is an image from your res folder

Categories

Resources