Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make stroke for 3 sides for a shape in android?

Tags:

android

I am using a stroke to make a colored border to a shape in xml layout in android .

Can I make the stroke for only 3 edges ( left,top,bottom) the right NO ?

like image 659
Adham Avatar asked Jul 02 '11 17:07

Adham


People also ask

What is Android stroke?

Stroke (outline around the 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.

How do you add a border in XML?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.


1 Answers

You can achieve this by using a layerlist and messing with the padding. You'll need 3 elements:

1: A border.xml shape, which is just a solid shape in the color of your border: border.xml

<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">     <solid android:color="#ff0000"/> </shape> 

2: The 'inner' shape, the shape where you want the border to appear around: inner.xml

<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">     <solid android:color="#00ff00"/> </shape> 

3: A layer list, which will put these 2 on top of eachother. You create the border by setting the padding on the inner shape: layerlist.xml

<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/border"/> <item android:drawable="@drawable/inner"     android:top="3dp" android:right="0dp" android:bottom="3dp"     android:left="3dp" /> </layer-list> 
like image 194
Mopper Avatar answered Oct 07 '22 22:10

Mopper