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
Related
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
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
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/
I am trying to implement in XML a ShapeDrawable like this, but so far without success.
How do I make the Stroke visible only for two sides?
Is that even possible?
Otherwise what cloud I use (I seed it as background of a TextView).
Here is a solution which is using a LayerListDrawable:
background_white_lateral_border.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="#android:color/white" />
</shape>
</item>
<item>
<inset
android:insetLeft="1dp"
android:insetRight="1dp" >
<shape android:shape="rectangle" >
<solid android:color="#android:color/black" />
</shape>
</inset>
</item>
</layer-list>
Note: This firstly draws a white rectangle and then a black rectangle with a left and right inset of 1dp on top of it, giving the effect of lateral borders. It's just something to keep in mind, in case you worry about performance (which is in my opinion negligible for minor styling like this).
AFAIK, that is not possible with a single ShapeDrawable. A nine-patch PNG, or a LayerListDrawable of two ShapeDrawables (one per line) should work though.
I'm trying to define a background drawable in XML that will make the background have a 1dp grey border on the left. The XML I'm using is:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00FFFFFF" />
<stroke android:width="1dp" android:color="#CCCCCC" />
<padding android:left="1dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
In the screenshot below you can see that it's actually putting a 1dp border around the entire view (the "Recent Lessons" area):
Can someone explain to me what I've done wrong here?
I think that you may have confused padding and stroke. The 1dp stroke you are adding is the border you see around the shape - not the padding. Try following these examples. For more about LayerList see the android docs (LayerList section). Basically, it boils down to multiple drawables as one.