Add stroke on button in android.
Border is basically applied where lots of buttons present on single application screen and developer wants to separate them with the use border. Border is applied on button make use of external layout .XML file. You just have to create additional layout file and set that as background on button. So here is the complete step by step tutorial for Add border to button in android.
Add border to button in android.
1. Create button_border_settings.xml file on layout folder.
Put below code into button_border_settings.xml file.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#55dcf9"/> <stroke android:width="3dp" android:color="#fd0261" ></stroke> </shape> </item> </selector>
Code for MainActivity.java file.
package com.android_examples.com.borderonbutton; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.borderonbutton.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="174dp" android:text="Android Button" android:background="@layout/button_border_settings" android:padding="20dp" /> </RelativeLayout>
Screenshot: