Set/Add border around ImageButton image in android

How to implement stroke around image button image using external custom xml layout file.

In this tutorial we are creating image button with border with custom XML layout file. So here is the complete step by step tutorial for Set/Add border around ImageButton image in android.

android-project-download-code-button

Note : Download image button image from below and put inside drawable-hdpi folder.

drawable hdpi folder

This is sample image button image.

demo_img

How to Set/Add border around ImageButton image in android.

Code for MainActivity.java file.

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

 <ImageButton
 android:id="@+id/imageButton1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:src="@drawable/demo_img"
 android:background="@layout/imagebutton_border"
 android:padding="10dp" />

</RelativeLayout>

Code for imagebutton_border.xml file.

 <shape xmlns:android="http://schemas.android.com/apk/res/android">
 <stroke
 android:width="2dp"
 android:color="#fec603" />
</shape>

Screenshot:

Set/Add border around ImageButton image in android

Click here to download Set/Add border around ImageButton image in android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *