Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border color on Android button

Tags:

I have a button created and have set the background color and text color as can be seen below. My question is: how do I set the buttons border color? I want to set the border color to white

Here is the button in my res -> layout -> main_nav.xml

<Button          android:id="@+id/btn_emergency"         style="@style/buttonStyle"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="Contact and Emergency"          android:onClick="onClickHandleStates" /> 

And here is its associated style in res -> values -> styles. The first 2 "items" work fine on their own. The final "item" was my attempt at changing button border to white without success.

<!-- The button styles --> <style name="buttonStyle">     <item name="android:textColor">#ffffff</item>     <item name="android:background">#80000000</item>      <item name="android:button">         <shape             android:shape="rectangle" >             <stroke                 android:width="0.5dp"                 android:color="#ffffff" />           </shape>     </item> </style> 
like image 753
heyred Avatar asked Sep 25 '13 16:09

heyred


People also ask

How to give border to Button Android?

We cannot add a border to an Android button using Button view attributes, to do so we need to create an XML file in the drawable folder and set this drawable XML as background to the Button view.

How do you change the color of the border on Android?

To add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. <stroke> tag is used to set the border width and color.

How to give Button border in Android studio?

Create a button_border. xml file in your drawable folder. And add button to your XML activity layout and set background android:background="@drawable/button_border" .

How can I make my Android button more attractive?

Using colors (background, text, border) Using custom shapes like circle, rounded corners and more. You can add images to your buttons to customize them. Using drawables to make gradients, dotted borders and more.


1 Answers

Use the <stroke> element. Add this xml file in res/drawable folder as button_border.xml:

 <?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="#ffffff" />  </shape> 

then call this by

<Button    android:id="@+id/button1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_margin="10dp"    android:background="@drawable/button_border"    android:text="Button"  /> 
like image 119
Shivang Trivedi Avatar answered Sep 19 '22 14:09

Shivang Trivedi