How to set up only circle size(in xml)? - android

I want to set up circle size, but I don't want resize other elements in layer list.
I have this 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/drawer_color" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient
android:endColor="#1b2727"
android:centerColor="#1a2a29"
android:startColor="#154440"
android:type="linear"
android:angle="90"/>
<size
android:height="600dp"
android:width="600dp" />
<stroke
android:width="3dip"
android:color="#color/drawer_ring_color" />
</shape>
</item>
</layer-list>
What I found:
And what I want to see:
How to do that?
Thanks!

Unfortunately this do not works this way, your drawable will take the size of view where you use that drawable.

Try to set padding in your code.
<item>
<shape android:shape="rectangle">
<solid android:color="#color/drawer_color" />
<padding
android:left="10dp"
android:top="100dp"
android:right="10dp"
android:bottom="100dp" />
</shape>
</item>

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 xml shape fills the whole background

I made a shape for a custom button and it fills the whole background of the button instead of showing the shape.
This is the xml code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="ring"
android:useLevel="false"
android:thicknessRatio="0">
<solid android:color="#color/white" />
<size
android:width="1dp"
android:height="1dp" />
</shape>
</item>
<item android:left="0.75dp" android:top="1dp" android:bottom="0.5dp" android:right="1.5dp">
<rotate android:fromDegrees="" android:pivotX="0%" android:pivotY="0%" android:toDegrees="0">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<size android:height="1dp" android:width="1dp"/>
</shape>
</rotate>
</item>
</layer-list>
I found the solution. For some reason when I use the code I posted in an Imageview it shows correctly, but if I try to use it for a background (doesn't matter if it is for an Imageview/button/etc it shows as a square.
The problem seemed to be that the Preview doesn't represent at all the shape as a background, so I had to check the layout all the time and do the changes depending on it, and not the preview.
This is the final code:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/white" />
<size
android:width="5dp"
android:height="5dp" />
</shape>
</item>
<item android:left="8dp" android:top="30dp" android:bottom="8dp" android:right="30dp">
<shape android:shape="rectangle">
<solid android:color="#color/white" />
<size android:height="1dp" android:width="1dp"/>
</shape>
</item>
</layer-list>

Gradiant Custom Shape in Android XML

I am looking for some tutorial or some reference which can help me to create some custom shapes like below-mentioned shape.Background Shape
I have tried creating a shape using the standard shape in android xml, but I am not able to find an appropriate solution for it.
It would be a good help if someone can explain how can I achieve this.
Try using this layer-list in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" android:tint="#0000ff">
<padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp"></padding>
</shape>
</item>
<item>
<bitmap
android:gravity="right"
android:src="Your_blur_backgound_image" />
// you can use any other image here
</item>
You should use a <layer-list>, try 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="#ffffff"/>
<size android:height="30dp" android:width="40dp"/>
</shape>
</item>
<item>
<shape
android:shape="oval">
<gradient
android:type="radial"
android:gradientRadius="15dp"
android:endColor="#ffffff"
android:startColor="#0000ff" />
</shape>
</item>
<item>
<inset
android:insetLeft="5dp"
android:insetRight="15dp"
android:insetTop="5dp"
android:insetBottom="5dp">
<shape android:shape="rectangle">
<solid android:color="#ff0000"/>
</shape>
</inset>
</item>
</layer-list>
Result:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="0"
android:centerX="0.1"
android:centerY="0.1"
android:centerColor="#1976d2"
android:startColor="#00e5ff"
android:endColor="#6200ea"
android:gradientRadius="100"
android:type="linear"/>
<padding android:left="5dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
<corners android:radius="6dp" />
</shape>

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

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