How to create shapes on drawable layout android - android

I need to create a layout that will be with rounded corners, but in which there will be various shapes.
I have a sample (in the screenshot below). I created a layout with rounded corners and the right color, but I don’t know if I can create a few more shapes on it (these shapes are dark in the screenshot) and if I can place them where I want?
My code for background shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/purple_light" />
<corners android:radius="16dp" />
</shape>

You can use android layer list drawable. You can refer the docs here.

You have 2 choices:
1- Export the complete shape as a vector (SVG) and import it into Android Studio.
Then you can use this shape wherever you want.
(Whole purple background with triangle shaped inside of it)
2- Trying to draw that shape on Android Studio.
You have to do something like this:
<?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/purple_200" />
<corners android:radius="1dp" />
</shape>
</item>
<item android:drawable="#drawable/ic_baseline_square_24"/>
<item android:drawable="#drawable/ic_baseline_triangle"/>
</layer-list>
But you need to completely design shapes with the drawable.
I tried quickly just for showing you the result:
Sample output

Related

How to auto mirror a shape in Android XML

I have a custom card like shape as below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorListItemBackground" />
<corners
android:bottomLeftRadius="16dp"
android:topLeftRadius="16dp" />
</shape>
My app also supports RTL languages, thus changing to RTL, this shape does not change(Auto Mirror).
What changes can I do to make it an auto mirror?
Notice the rounded corners near the circle.
LTR_Image
The rounded corners still in the same place.
RTL_Image
You can have define custom shape inside
res/
drawable
drawable-ldltr
drawable-ldrtl
Now put the mirrored custom shape inside drawable-ldrtl and it should work.
Your custom shape xml should be exactly opposite i.e.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorListItemBackground" />
<corners
android:bottomRightRadius="16dp"
android:topRightRadius="16dp" />
</shape>
You can alternatively add android:autoMirrored="true" to your vector drawable which should auto mirror it. But this requires you to have a vector image as per documentation : https://developer.android.com/reference/android/graphics/drawable/VectorDrawable
Hence a shortcut is to wrap your shape around with a vector drawable
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/your_shape"
android:autoMirrored="true"/>
Let me know if this works for you.

How do I make a launcher icon with a gradient background?

The standard Android Image Studio only allows me to select a single color as the background for my icon. Is there a method for selecting two colors and creating a linear gradient effect?
Create an color_gradient.xml in drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#color/start_gradient" <!--Start color of the gradient-->
android:endColor="#color/end_gradient" <!--End color of the gradient-->
android:angle="45" /> <!--Change this to see the combination of gradient color-->
</shape>
</item>
</selector>
When using for your image:
android:backgroundTint="#drawable/color_gradient
EDIT:
In case of using Android Image Asset, you still can link the background color to this xml file, to create a gradient background

Android round image with custom corners

for rounding image corners there are many library and released, but i want to round custom corners such as only TopLeft and BottomRight but i cant find this library on Android Arsenal or github. for example see this screen shot
all of released libraries rounding all corners, and i dont like it
If you want to use such rounded corners (only 2 out of 4), I'd suggest creating .png file with corners as you want them, 9patching images to stretch properly and setting created file as background to your layout (CardView, any layout you use...)
Set below xml as background of your main layout of listview raw file.
corner.xml
<?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="#ffffff" />
<corners android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
</shape>
</item>
</layer-list>
Hope it helps!!!

Drawing circular gradient shade outlining shape

So, I have a white circular shape described in the following code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/color_white"/>
<stroke android:width="1dp" android:color="#color/color_white" />
<size android:width="10dp" android:height="10dp"/>
</shape>
I've been trying to produce an outlining like this, with a gradient (notice that there is a shadow with gradient surrounding it):
I've tried using a layer-list, but it didn't work, because one image would be on top of the other and the background image would not appear.
My idea would be to have the white circular shape and the shadow in the background, slightly bigger than the white one, producing the outlining.
Could someone help me?
I created this drawable and it looks like

Android ShapeDrawable stroke is blurred

I created a rather simple shapedrawable with semi-transparent borders, and used it as a background for two adjacent view.
Either if the srtoke color is partially transparent, I'm expecting a solid stroke (like the image on the right), but what I get is a blurred stroke (image on the left).
What I'm doing wrong?
(images are taken from the ADT preview, in the emulator the effect is even more visible)
This is the ShapeDrawable (theme_base_bg_framed.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/base_frame_solid_color"
/>
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
<padding
android:left="#dimen/frame_padding_size"
android:top="#dimen/frame_padding_size"
android:right="#dimen/frame_padding_size"
android:bottom="#dimen/frame_padding_size"
/>
</shape>
It uses these color and dimen definitions:
<color name="base_frame_solid_color">#20ffffff</color>
<color name="base_frame_border_color">#40ffffff</color>
<dimen name="frame_border_size">2dp</dimen>
<dimen name="frame_padding_size">2dp</dimen>
Both drawables are assigned to the Views background
<style name="ViewWithBorder">
<item name="android:background">#drawable/theme_base_bg_framed</item>
</style>
Edit:
The colors used in the ShapeDrawable are alphaed for a reason. The view in the background is going to contain other views and/or images. This is a better example of what I get (left) and what I'm expecting to get (right).
I use this values to get the result you wanted:
colors.xml:
<color name="base_frame_solid_color">#20000000</color>
<color name="base_frame_border_color">#40ffffff</color>
shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/base_frame_solid_color"
/>
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
</shape>
Hope this helps!
Ok, I found what's happening and how to fix it.
It seems that Android, when filling a ShapeDrawable with a border, doesn't fill it to it's full size but just to the middle of the stroke. So, with a stroke of 2dp, it leave a space of 1dp all around. This can be noticed only when using alphaed borders, like I'm doing, because normally the border cover it.
Tho fix this behaviour, I used a LayerList drawable containing two ShapeDrawables, one for the solid color (with no borders) and one for the stroke (with no solid color):
<?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/base_frame_solid_color"
/>
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<stroke
android:width="#dimen/frame_border_size"
android:color="#color/base_frame_border_color"
/>
<padding
android:left="#dimen/frame_padding_size"
android:top="#dimen/frame_padding_size"
android:right="#dimen/frame_padding_size"
android:bottom="#dimen/frame_padding_size"
/>
</shape>
</item>
</layer-list>
It works, but being the border superimposed to the "solid" background, it's color need some adjustment.

Categories

Resources