Drawing a rectangle inside an oval doesn't give the desired result - android

I tried to make a button wherein a square shape should be inside a circle shape. It works well on a square with a circle but not with the other way around and I wonder why. Here's my xml code so far for this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="#dimen/tab_space" android:right="#dimen/tab_space">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<size android:width="80dp" android:height="80dp" />
<gradient android:angle="-90" android:startColor="#990000" android:endColor="#550000" />
<stroke android:width="1dp" android:color="#110000" />
</shape>
</item>
<item android:left="#dimen/tab_space" android:right="#dimen/tab_space">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:width="50dp" android:height="50dp" />
<gradient android:angle="-90" android:startColor="#ff0000" android:endColor="#990000" />
<stroke android:width="1dp" android:color="#440000" />
</shape>
</item>
</layer-list>
Any solution for this?

You can try this. It worked for me.
chat_icon.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<padding android:left="7pt" android:top="8pt" android:right="7pt" android:bottom="8pt"/>
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:color="#color/jade" android:width="2dip"/>
<solid android:color="#color/white" />
</shape>
</item>
Then, to use it in your layout, declare it as follows:
<ImageView
android:id="#+id/chat_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chat_icon"
------------------------------ />

Related

How to achieve custom shape match parent?

I created custom shape in XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<size android:width="160dp"/>
<size android:height="30dp"/>
<solid android:color="#color/white"/>
<padding android:right="15dp"/>
<stroke android:width="1dp" android:color="#color/main_color"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item
android:drawable="#drawable/down_arrow_red"
android:width="10dp"
android:height="6dp"
android:gravity="center|right"/>
</layer-list>
</item>
</selector>
Since match parent wasn't allowed while defining of width and height, I am worried how this shape will look like on different size screens. How to make this shape match parent screen? Thanks!
I think what you want to ask is about sizing.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<item>
<shape
android:layout_width="match_parent"
android:shape="rectangle">
<size android:height="30dp" />
<solid android:color="#color/white" />
<padding android:right="15dp" />
<stroke
android:width="1dp"
android:color="#color/teal_200" />
<corners android:radius="15dp" />
</shape>
</item>
<item
android:width="10dp"
android:height="6dp"
android:drawable="#android:drawable/arrow_down_float"
android:gravity="center|right" />
</layer-list>

Android drawable xml reverse corners

I'm trying to replicate the following shape in Android studio:
I can't figure out how to do it in android drawable xml.
At the moment I used a layer-list where:
As the first item I placed a transparent (or white) rectangle, to which I gave a padding on the left side and vertically.
As the second item I placed a rectangle containing the gradient and the corners on the left.
Now, I don't know how I can make the two "reverse angles" on the right, I've tried everything but online there aren't many examples to refer.
Here's my current code:
<?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/transparent" />
<padding
android:bottom="8dp"
android:left="8dp"
android:right="0dp"
android:top="8dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#color/green"
android:startColor="#color/blue"
android:type="linear" />
<corners
android:bottomLeftRadius="22dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="22dp"
android:topRightRadius="0dp" />
</shape>
</item>
</layer-list>
How can I change the code to obtain this shape?
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="600dp"
android:height="400dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:bottomRightRadius="100dp" />
</shape>
</item>
<item android:width="600dp"
android:height="100dp"
android:top="300dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
<corners android:topRightRadius="100dp" />
</shape>
</item>
<item android:width="200dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white" />
</shape>
</item>
<item android:width="600dp"
android:height="200dp"
android:top="100dp">
<shape android:shape="rectangle">
<gradient
android:angle="0"
android:endColor="#android:color/holo_green_light"
android:startColor="#android:color/holo_blue_light"
android:type="linear" />
<corners android:radius="100dp" />
</shape>
</item>
</layer-list>
Here is a preview image
try this

CardView Shadow like this?

I want to make like this. How to get exactly like this (see image below)? I tried layer-list but not able to get this view.
<?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="#afbdc3" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:right="5px">
<shape android:shape="rectangle">
<solid android:color="#91a5af" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:right="10px">
<shape android:shape="rectangle">
<solid android:color="#79919d" />
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
And so on
LIKE THIS LAYER

Custom shape in android using xml

I am trying to draw a custom shape which I can use as a background for my layout. But I am not able to do so. Is it possible to draw a shape as below using xml in android. I am not getting how to cut the semicircle shape from the vertical centres of rectangle.
Use layer-list to make this custom shape drawable.
/res/drawable/custom_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp" />
<size
android:width="300dp"
android:height="100dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>
<!-- Left Half-Circle -->
<item
android:left="-10dp"
android:right="290dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Right Half-Circle -->
<item
android:left="290dp"
android:right="-10dp"
android:top="40dp"
android:bottom="40dp">
<shape android:shape="oval">
<solid android:color="#000000" />
</shape>
</item>
<!-- Middle Dotted Line -->
<item
android:left="10dp"
android:right="10dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"
android:color="#ff00"/>
</shape>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
OUTPUT:
Hope this will help~
In your case it might be worth to create a resizable bitmap (9-Patch drawable). Follow this guide.
try bellow xml. save it in drawable.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<solid
android:color="#ffffff"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="1dp"
android:color="#000000"
/>
</shape>
It is better to use 9-Patch images. If you can create an image like above you can use https://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create 9-patch image(define expandable area).
Try this xml file in drawable :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="#color/primary" />
<corners android:radius="50dp"/>
</shape>
Please refer the below xml, it may help you out
<?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="#android:color/black" />
</shape>
</item>
<item
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="200dp"
android:height="200dp" />
<solid android:color="#color/colorAccent" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="line">
<solid android:color="#android:color/black" />
<stroke android:width="1dp"
android:dashWidth="10px"
android:dashGap="10px"/>
</shape>
</item>
<item
android:bottom="103dp"
android:left="-10dp"
android:right="205dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
<item
android:bottom="103dp"
android:left="205dp"
android:right="-10dp"
android:top="103dp">
<shape android:shape="oval">
<size
android:width="3dp"
android:height="3dp" />
<solid android:color="#android:color/black" />
<stroke android:width="1dp" />
</shape>
</item>
</layer-list>
Try below code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="3dip" />
<stroke
android:width="1dp"
android:color="#ED2A3C" />
</shape>

Shadow-like drawable, using "layer-list"

Background
I'm trying to make a (fake) shadow below a view, so that the layers would be vertical in this manner:
a line of color "#43000000" with a height of "1dp"
below the line, a gradient that starts from the top with color "#1A000000" and ends with color "#00000000", and is of a height of "8dp".
The problem
For some reason, I can't draw the line correctly.
No matter what I do, the line for some reason takes the whole space, while the gradient seems fine.
What I tried
I tried using "line" as the shape and also "rectangle". I also tried using "stroke" instead of "solid", tried adding padding and margins...
Here's the XML of the drawable file:
<?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="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#1A000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Again, this is not the only thing I've tried. It's just my first attempt.
The question
How can I fix this XML ? what is causing it to happen?
Here's what I've found to be working:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:bottom="8dp">
<shape android:shape="rectangle" >
<size android:height="1dp" />
<solid android:color="#43000000" />
</shape>
</item>
<item android:top="1dp">
<shape
android:dither="true"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#10000000" />
<size android:height="8dp" />
</shape>
</item>
</layer-list>
Tried with this,may it fulfill your need with gradient
<?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="#e040fb"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#E1BEE7"/>
<corners android:radius="2dp" />
<gradient
android:angle="270"
android:endColor="#e1bee7"
android:startColor="#e040fb" />
</shape>
</item>
</layer-list>
You can change the heights in this and get what you want
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:height="10dp"
android:gravity="bottom">
<shape
android:shape="oval">
<solid android:color="#color/shadow_color" />
</shape>
</item>
<item android:right="20dp" android:left="20dp" android:bottom="12dp"
android:top="10dp">
<shape
android:shape="rectangle">
<solid android:color="#color/border_color"/>
<corners android:radius="20dp"/>
</shape>
</item>
<item android:right="30dp" android:left="30dp" android:bottom="23dp"
android:top="20dp">
<shape
android:shape="rectangle">
<solid android:color="#color/base_color"/>
<corners android:radius="15dp"/>
</shape>
</item>
</layer-list>

Categories

Resources