Add dash dotted border around to button in android

Create android buttons with dash dotted border effect using dashWidth and dashGap tag.

Dash dotted border can be easily created through external layout xml file by setting up that particular file to button background. Developer can set dashed border color, specify gap between dash, dash itself width, border color using specific tags. So here is the complete step by step tutorial for Add dash dotted border around to button in android.

android-project-download-code-button

How to Add dash dotted border around to button in android.

Please create other two layout files in res-> layout folder.
Code for MainActivity.java file.

package com.android_examples.com.buttonwithdashdottedborder;
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.buttonwithdashdottedborder.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Dash border button"
 android:background="@layout/dash_border"
 android:padding="10dp" />

 <Button
 android:id="@+id/button2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/button1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="16dp"
 android:text="Dotted Border Button"
 android:background="@layout/dotted_border"
 android:padding="10dp" />

</RelativeLayout>

Code for dash_border.xml file.

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#7bd1fd"/>
 <corners android:radius="10dp" />
 <stroke
 android:width="3dp"
 android:dashWidth="12dp"
 android:color="#014264"
 android:dashGap="5dp"
 />
 </shape>
</item>
</selector>

Code for dotted_border.xml layout file.

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#7bd1fd"/>
 <corners android:radius="10dp" />
 <stroke
 android:width="3dp"
 android:dashWidth="2dp"
 android:color="#014264"
 android:dashGap="4dp"
 />
 </shape>
</item>
</selector>

Screenshot:

Add dash dotted border around to button in android

Click Here To Download Add dash dotted border around to button in android project.