jeffreyveon jeffreyveon - 9 days ago 4
Android Question

Android - border for button

How do I add a border to a button? Is it possible to do this without resorting to use of images?

Answer

You can define your button as a shape as dymmeh points out. To give it a border, use the <stroke> element (name the file my_button_bg.xml and place it in res/drawables):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <gradient android:startColor="#FFFFFF" 
    android:endColor="#00FF00"
    android:angle="270" />
  <corners android:radius="3dp" />
  <stroke android:width="5px" android:color="#000000" />
</shape>

Then use the file like so:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your Text"
    android:background="@drawable/my_button_bg"
    />