How do you add a background image to a shape? The code I tried below but no success:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
//here is where i need to set the image
<solid android:color="#drawable/button_image"/>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
Use following layerlist:
<?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">
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
<item android:drawable="#drawable/image_name_here" />
</layer-list>
I used the following for a drawable image with a circular background.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item
android:drawable="#drawable/ic_select"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"/>
</layer-list>
Here is what it looks like
This is a circle shape with icon inside:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ok_icon"/>
<item>
<shape
android:shape="oval">
<solid android:color="#color/transparent"/>
<stroke android:width="2dp" android:color="#color/button_grey"/>
</shape>
</item>
</layer-list>
This is a corner image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/img_main_blue"
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<item>
<shape
android:padding="10dp"
android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="5dp"
android:color="#color/white" />
</shape>
</item>
</layer-list>
I used the following for a drawable image with a border.
First make a .xml file with this code in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/orange"/>
</shape>
</item>
<item
android:top="2dp"
android:bottom="2dp"
android:left="2dp"
android:right="2dp">
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>
<item
android:drawable="#drawable/messages" //here messages is my image name, please give here your image name.
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp"/>
Second make a view .xml file in layout folder and call the above .xml file with this way
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/merchant_circle" /> // here merchant_circle will be your first .xml file name
Here is another most easy way to get a custom shape for your image (Image View). It may be helpful for someone. It's just a single line code.
First you need to add a dependency:
dependencies {
compile 'com.mafstech.libs:mafs-image-shape:1.0.4'
}
And then just write a line of code like this:
Shaper.shape(context,
R.drawable.your_original_image_which_will_be_displayed,
R.drawable.shaped_image_your_original_image_will_get_this_images_shape,
imageView,
height,
weight);
Related
Here is the image which I want to design in android studio.
This will give you the same image that you're looking for.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#color/black" />
<stroke
android:width="16dp"
android:color="#color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke
android:width="8dp"
android:color="#color/black" />
</shape>
</item>
</layer-list>
You can achieve the required output by using layer-list. In the drawable folder create a new Drawable Resource File named background.xml and replace with the following code in the file.
This is the code for background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<solid android:color="#5271ff" />
<stroke
android:width="20dp"
android:color="#f5fffe" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#4d75f9" />
</shape>
</item>
</layer-list>
I'm trying to create a drawable with two shapes next to each other using layer-list, how would I do it?
You can use the following code to have the drawable you want(Change the colors and height as you want)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="center">
<shape android:shape="rectangle">
<size
android:width="5dp"
android:height="5dp" />
<solid android:color="#color/colorAccent"/>
</shape>
</item>
<item android:gravity="right">
<shape android:shape="rectangle">
<size
android:width="1dp"
android:height="5dp" />
<solid android:color="#color/colorPrimary"/>
</shape>
</item>
</layer-list>
I need to change the background color of a button using shapes.
`
<?xml version="1.0" encoding="utf-8"?/>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_active="true">
<shape android:shape="rectangle">
<solid android:color="#color/blue3"/>
<stroke android:color="#color/white1" android:width="2dp" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
`
I am trying to set blue3 color using solid but it's not appearing in view. Please make me correct if i am trying to do it incorrect way.
Use this file in drawable
<?xml version="1.0" encoding="utf-8"?>
<item>
<layer-list >
<item>
<shape>
<solid android:color="#d4d4d4"></solid>
<corners android:radius="2dp"></corners>
</shape>
</item>
<item android:right="2dp" android:bottom="2dp" android:left="1dp" android:top="1dp">
<shape>
<solid android:color="#ff0000"></solid>
<corners android:radius="2dp"></corners>
</shape>
</item>
</layer-list>
</item>
and set Button background from drawable
Hope this will help!
Here is the solution of this question.
`
<?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="#color/blue3" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
`
I am trying to create a following shape in android.
I tried following code but it's only for changing the corners
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffff" />
<gradient
android:startColor="#00EBCF"
android:endColor="#00BEEC"
android:angle="180"
android:type="linear" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
What do I need to make changes in code to achieve the above shape?
Edit1
As per the answer I used following code (modifications in layout height):
<?xml version="1.0" encoding="utf-8"?>
</shape>
</item>
<item
android:width="1500dp"
android:height="200dp"
android:top="95dp">
<shape android:shape="oval">
<solid android:color="#36D6E9" />
</shape>
</item>
But when I am using this layout as a background for LinearLayout. It's showing me this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/header_shape_rect">
use this as an example
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:height="200dp">
<shape android:shape="rectangle">
<solid android:color="#ccc"/>
</shape>
</item>
<item android:height="200dp" android:top="95dp" android:width="1500dp" >
<shape android:shape="oval">
<solid android:color="#ccc"/>
</shape>
</item>
</layer-list>
I have a drawable xml file which draws a circle.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666" />
<size
android:width="120dp"
android:height="120dp" />
</shape>
now, I have to add a image in drawable folder. please help me where to change the code.
You can use a layer list like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
</item>
<item android:drawable="#drawable/your_drawable"/>
</layer-list>