Set a border in corners of layout - android

I want to add border like the one in the image below. I tried achieve this using xml drawables' layer-list component, but I couldn't.

Create an XML file named border.xml in the drawable folder and put the following code in it.
<?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="#FF0000" />
</shape>
</item>
<item android:left="5dp" android:right="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
</layer-list>
Then add a background to your linear layout like this:
android:background="#drawable/border"
Finally its works perfectly with all APIs
Let me know if it was usefull Behzad Fartash

create new drawable resource file into the drawable folder and add below code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:radius="#dimen/_10sdp"
</shape>
and set this drawable as the background of your parent layout

Related

Drawable with more than two colors in XML?

How to achieve this in drawable in XML android?
Bottom is transparent.
To achieve this in drawable in XML.
Firstly, you need to create a drawable file.
Then, add these lines of code.
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#00000000">
<item>
<shape android:shape="rectangle">
<padding
android:left="10dp"
android:right="10dp"/>
<gradient
android:startColor="#color/purple_500"
android:endColor="#color/purple_200"
android:angle="0"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#color/white"
android:angle="90"/>
</shape>
</item>
</ripple>
Then use the drawable file anywhere in android.

How to append shadow effect to imageview in android?

I want to show imageview like 3d image with shadow effect please help me
Create one xml layout file under res -> drawable folder. Lets name it "image_shadow.xml", then copy below code to it.
<?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/darker_gray" />
<corners android:radius="0dp"/>
</shape>
</item>
<item android:right="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="1dp"/>
</shape>
</item>
</layer-list>
And then apply it within your ImageView as
android:background="#drawable/image_shadow
Hope it might help you.

How can I create a vertical line in a drawable xml? Android

Is there a way to make a drawable that draws a vertical line? So far I have a horizontal line that draws this:
current code 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/object_switcher_background" />
<item android:left="0dp"
android:right="250dp">
<shape android:shape="line">
<stroke android:color="#color/c19"
android:width="2dp"/>
<size android:width="40dp"/>
</shape>
</item>
<item android:left="50dp"
android:bottom="50dp"
android:top="75dp"
android:right="150dp">
<shape android:shape="line">
<stroke android:color="#color/c19"
android:width="2dp"
android:rotation="90"/>
<size android:width="40dp"/>
</shape>
</item>
</layer-list>
How can I manipulate this to make it look like this?
You could do this by nesting your second item inside a rotate item.
you can do that using rotate item: the below code defines a white background with a vertical divider using only xml drawable
<?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>
<rotate android:fromDegrees="90">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#android:color/darker_gray" />
</shape>
</rotate>
</item>
I'm not sure why you need a drawable for this.
This can also be done by using a simple view and adjusting its height, width and position according to your needs. For example:
<View
android:layout_width="1dp"
android:background="#color/grey_text_color"
android:layout_height="match_parent"/>

Create shape for drawable

I have ListView, and i have a background for the list items:
Is there any way to recreate this background as a shape in drawable XML? The most outer grey is not part of the item background, it is actually the ListView.
You can try following with the values of your choice. It will make the bottom two corners rounded.
rounded_rectangle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270" android:endColor="#404040" android:startColor="#404040" />
<stroke android:width="1dp" android:color="#808080" />
<corners android:bottomLeftRadius="11dp" android:bottomRightRadius="11dp"/>
</shape>
</item>
</selector>
then use it as a background to your listitem as follows
android:background="#drawable/rounded_rectangle"
You can try this shape drawable:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#606060"/>
<stroke android:color="#303030" android:width="2dp" />
</shape>

How to add a 9patch image to a xml drawable

I have the following drawable which draw a rectangle. Can you please tell me how can I add a 9patch image as the background of this drawable?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<stroke android:width="1dp" android:color="#FFFFFFFF" />
<solid android:color="#android:color/transparent"/>
</shape>
Thank you.
You can't use a drawable for that, but you can use a
<layer-list>
<item android:drawable="#drawable/mydrawable" />
<item>
<shape>...</shape>
</item>
</layer-list>

Categories

Resources