What do <stroke /> and <corners /> mean? - android

I want to know what do exactly Stroke and Corners do in Android Studio.
This code is in drawable folder for image button in main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<shape>
<stroke android:width="1.3dp">
</stroke>
<corners android:radius="30dp">
</corners>
</shape>

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.
Since your shape is a rectangle, you can round rectangle’s corners. You can do that inside of the corners tag. You can specify the radius for all of the corners using android:radius
you can see useful Content and samples about android shapes in https://android.jlelse.eu/android-shape-drawables-tutorial-17fbece6fef5

stroke : define shape bolder style.
corners: Define shape corner style.
Please read this article https://www.dev2qa.com/android-shape-selector-examples/

Related

How to create shapes on drawable layout 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

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.

Shadow resulting from elevation showing with circle drawable as background but not with rectangle

I'm trying to use a View's elevation property to cast a shadow. It works fine when the background is a circle or a rounded rectangle drawable. However, if I use a color or a rectangle drawable as a background the shadow doesn't show.
Simply changing the radius of the rounded rectangle to zero, causes the shadow to disappear. I have tried solutions to similar issues such as adding padding and margin. I added both:
android:clipChildren="false"
android:clipToPadding="false"
to the parent View but that didn't fix the problem either.
I am using the simplest shapes possible:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#color/colorAccent" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorAccent" />
</shape>
And here's the very simple layout where I'm testing this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#drawable/rectangle"
android:elevation="40dp"/>
</LinearLayout>
I know there are other ways of achieving shadows in Android such as using 9-patch images, but elevation is a much simpler way and I'm hoping to make it work using only that.
These are the previews changing only the background drawable.
https://i.imgur.com/CtVhI8m.png
https://i.imgur.com/T2Ozcz7.png
This is the end result I'm going for:
https://i.imgur.com/irIMNAF.png
But I mostly just want to figure out why the shadow is not showing with a color or rectangular drawable as background.
When I use a solid color background (e.g. android:background="#fff") or a rectangle shape without rounded corners and run the app, I see a shadow.
However, I have to actually run the app. Just looking at the Android Studio layout preview doesn't show the shadow unless I use a background drawable with rounded corners. So I suspect you're just looking at a bug (or feature?) of the layout preview.
In my case helped wrap my shape with layer-list like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/avatar_ripple">
<item>
<shape android:shape="oval">
<solid android:color="#color/black_light" />
<stroke
android:width="#dimen/stroke_large"
android:color="#color/gray" />
</shape>
</item>
Then you can simple apply elevation to your view with your drawable as background, but there is bug that you won't see the shadow until you launch the app.
Particularly helped this answer: https://stackoverflow.com/a/60424218/16006976

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