Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create gradient border in android?

Tags:

android

border

How can I create a gradient border like in the image below?

border

like image 218
ida Avatar asked Aug 23 '12 18:08

ida


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"

Specify here where you want to have a stroke(top, left, right, bottom)

android:top="3dp" android:right="0dp" android:bottom="3dp"
android:left="3dp" />

Set this as the background of your TextView, Layout etc. (where you want the stroke to appear)

Or just create a 9Patch image with the borders.

like image 84
Ahmad Avatar answered Sep 19 '22 19:09

Ahmad