Custom shape(rounded square) drawable android - android

I want custom shape border like the one shown below:
This is what i have tried so far:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000"/>
<stroke android:width="0.1dp" android:color="#FFFFFF" />
<corners android:radius="5dp"/>
<!--<padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" />-->
</shape>
But this does not give me curvy sides,only curvy corners.
I need curvy sides

Look at this when change the drawable to this you will get the following output.
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="25dp"
android:src="#drawable/rectangle" />
rectangle.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<corners android:radius="20dp" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
ScreenShot :

Try like this.
Create a drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<stroke
android:width="3dp"
android:color="#android:color/white" />
<corners android:radius="17dp" />
</shape>
And then use it with any view.
<TextView
android:layout_width="50dp"
android:textColor="#android:color/white"
android:background="#drawable/rounded_corner"
android:layout_height="50dp"
android:gravity="center"
android:text="T" />
Change the radious and the height and width according to your need.
Please make sure the height and width of the view must be same if you want this type of effect.

Related

How to create a shadow border inside a LinearLayout or EditText in android?

How to add a shadow border on 4 sides of an edit text as shown in the image? Applied below code but it creates a shadow on single side only.
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFBDBDBD"
android:centerColor="#65FFFFFF"
android:endColor="#00FFFFFF"
/>
<stroke
android:width="1dp"
android:color="#C3C3C3" />
<corners
android:radius="5dp" />
</shape>
You can use elevation like :-
android:elevation="5dp"
elevation worked as Z-index.
You can try adding width and color inside the shape tag.
It will look like this:
<stroke android:width="3dp" android:color="#bfafb2"/>
First create this background drawable with a rectangle, round corner and a stroke in grey. Called background_drawable.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="5dp" />
<stroke android:width="1dp"
android:color="#C3C3C3"/>
</shape>
After that you set this as android:background="" in your LinearLayout and EditText, both widgets have android:elevation="2dp" for it's shadow on each sites matching the stroke of the rectangle. Here is your layout called activity_main.xml:
<LinearLayout
android:layout_width="200dp"
android:layout_height="100dp"
android:elevation="2dp"
android:id="#+id/linear_layout"
android:background="#drawable/background_drawable"
android:layout_centerInParent="true"
android:orientation="horizontal" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/edit_text"
android:elevation="2dp"
android:layout_marginTop="10dp"
android:background="#drawable/background_drawable"
android:layout_below="#+id/linear_layout"
android:layout_centerHorizontal="true"
android:padding="10dp"/>
The final result will look like this image.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="2dp"
android:right="2dp"
android:top="2dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>

Android drawable shape storke issue

Why are there opaque areas?
I made a custom drawable,
but the same phenomenon as the image occurred.
i test phone Samsung A30, Pixel 4a
drawable/alarm_list_reward_start.xml
<?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="2.0dp"
android:color="#000000" />
<solid android:color="#FFEB2A" />
<corners android:radius="10.0dp" />
</shape>
</item>
</layer-list>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="12.0dp"
android:layout_marginEnd="12.0dp"
android:layout_marginBottom="50.0dp">
<TextView
android:id="#+id/rewardBtn"
android:layout_width="match_parent"
android:layout_height="44.0dp"
android:focusable="false"
android:background="#drawable/alarm_list_reward_start"
android:gravity="center"
android:textColor="#000000"
android:textSize="15.0dp"
android:textStyle="bold" />
</RelativeLayout>
This is happening, as you are using android:layout_width="match_parent". You can solve this issue by using wrap_content. Or you could set custom width and height of the layer list, such as,
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size android:width="152dp" android:height="152dp" />
<solid android:color="#00FFFFFF" />
<stroke android:width="2.0dp" android:color="#000000" />
<solid android:color="#FFEB2A" />
<corners android:radius="10.0dp" />
</shape>
</item>
</layer-list>

How to set union shape with dynamic size and spect ratio in android drawable

I want to make a shape like in the below image. Size in the graphic is as below
The circle/Oval size is 30px
The rounded rectangle height is 20px and width is 65px
The rectangle start is set to the circle center so in total graphic size will be 80px.
However, for drawable I need responsive size so I can use it for any
size of the view.
I am able to create a shape by overlapping that gives a similar illusion. I am not sure if types of boolean (graphics) is possible in android or not.
Drawable XML
<?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:width="75dp"
android:height="30dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:right="60dp">
<shape android:shape="oval">
<solid android:color="#color/colorPrimary" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item android:left="15dp" android:top="10dp" android:bottom="10dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary" />
<size
android:width="66dp"
android:height="20dp" />
<corners
android:bottomRightRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</layer-list>
The above XML producing shape as in below screenshot
When I set the shape to View it output as in below screenshot
Layout XML
<View
android:layout_width="80dp"
android:layout_height="30dp"
android:background="#drawable/shape_flask"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Output
As you can see, even defining size in drawable and view shape doesn't stick to the size aspect ration. I am sure it is me who is doing anything wrong.
Question: How can I make the drawable shape that works on any size of view keeping aspect ration the same as it is in the Graphic
reference. And how to add Drop Shadow to it.
EDITED
I have fixed some size issue but the shape still has a problem. For better visualization, I set different colors for both shapes.
Drawable XML
<?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:width="80dp"
android:height="30dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:right="60dp">
<shape android:shape="oval">
<solid android:color="#color/colorPrimary" />
<size
android:width="30dp"
android:height="30dp" />
</shape>
</item>
<item android:left="15dp" android:top="5dp" android:bottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent" />
<size
android:width="66dp"
android:height="20dp" />
<corners
android:bottomRightRadius="20dp"
android:topRightRadius="20dp" />
</shape>
</item>
</layer-list>
But as you can see in the below screenshot of the view it doesn't output the same size and position. However, the View code is the same.
How about splitting drawables like this?
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="#+id/oval"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/shape_oval" />
<TextView
android:id="#+id/rec"
android:layout_width="65dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/oval"
android:layout_toRightOf="#+id/oval"
android:background="#drawable/shape_rec"
android:layout_marginStart="-15dp"
android:layout_marginLeft="-15dp" />
</RelativeLayout>
shape_oval
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorPrimary" />
</shape>
shape_rec
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />
<size
android:width="66dp"
android:height="20dp" />
<corners
android:bottomRightRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Preview

Rounded corner list view in android

I have a problem that I want to create a list view with rounded corners but I failed to achieve the same. Please suggest me for the right solution.
CustomShape XML:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke android:width="1dp" android:color="#ffbbbbbb" />
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
/>
</shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
<shape >
<solid android:color="#ffffffff" />
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
/>
</shape>
</item>
</layer-list>
main XML:
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
android:layout_marginBottom="10dp" android:layout_marginTop="120dip">
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/lv_homeScreen" android:cacheColorHint="#color/WHITE" android:dividerHeight="0dp" android:background="#drawable/customshape">
</ListView>
</LinearLayout>
I have attached the code which I am using for the same desire. Please have a look and share me if there are any flaws.
Thanks in advance.
set this container.xml (in drawable) as the background of your listview:
<gradient
android:startColor="#FFF"
android:centerColor="#FFF"
android:endColor="#FFFF"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#e98d59"/>
<corners
android:radius="10dp"/>

How to make buttons look something like a list

In my app I have buttons which look like this:
I want to make them look like this :
My background.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/solid" />
<item android:drawable="#drawable/shape" />
</layer-list>
solid.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ababab" />
</shape>
And the shape.xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="3dp" color="#ff000000" />
<corners android:radius="15dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
How can I get the desired appearance of the buttons?
Top shape:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="5dp"
android:topRightRadius="5dp" />
<padding
android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<solid
android:color="#ffffffff" />
<stroke
android:width="2dp"
android:color="#ff000000" />
</shape>
Middle shape:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<solid
android:color="#ffffffff" />
<stroke
android:width="2dp"
android:color="#ff000000" />
</shape>
Bottom shape:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp" />
<padding
android:left="3dp"
android:top="3dp"
android:right="3dp"
android:bottom="3dp" />
<solid
android:color="#ffffffff" />
<stroke
android:width="2dp"
android:color="#ff000000" />
</shape>
Example:
<Button
android:background="#drawable/top"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:background="#drawable/middle"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:text="Middle" />
<Button
android:background="#drawable/bottom"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="-2dp"
android:text="Bottom" />
Look at android:layout_marginTop="-2dp" - you need it to overlap border of top button with border of bottom button
Result (the graphic is not so good, sry):
After some tweaking you'll have a shape as you expect.
My tweaking:
You could do it with bitmaps or images. You'd need three images. One for the top button, one for the middle buttons, and one for the bottom button. Then just set the buttons to their appropriate background images.
I take a different approach to do this.
Usually I create a rounded corner background with will correspond to the background of the whole set of buttons. (Usually I have a linear layout wrapping them.) Then, there is a separator between each button.
That should do what you want in a simple way.

Categories

Resources