activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F4F0EC"
android:padding="16dp"
tools:context=".MainActivity">
<!-- ImageButton with default border, no border -->
<ImageButton
android:id="@+id/ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:src="@drawable/share_solid"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- ImageButton with border by XML -->
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:src="@drawable/share_solid"
android:padding="8dp"
android:background="@drawable/image_button_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ib" />
<!-- ImageButton with border programmatically -->
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:src="@drawable/share_solid"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ib2" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/drawable/image_button_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:color="#6D889C"
android:width="2dp"
/>
</shape>
</item>
</selector>
MainActivity.java
package com.cfsuman.androidtutorials;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
import androidx.core.content.ContextCompat;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the context
MainActivity context = this;
// Get the widgets reference from XML layout
ImageButton ib3 = findViewById(R.id.ib3);
// Set the background for third ImageButton
// This background is empty and have colored border
ib3.setBackground(ContextCompat.getDrawable(
context,
R.drawable.image_button_border
));
}
}

- Android horizontal ProgressBar example
- How to set ImageView width and height programmatically in Android
- How to set ImageView image from drawable in Android
- How to set ImageView image from URL in Android
- How to set DatePickerDialog cancel button click listener in Android
- How to format DatePickerDialog selected date in Android
- How to get AM PM value from TimePickerDialog in Android
- How to use ImageButton different image ScaleType in Android
- How to resize/scale an ImageButton in Android
- How to add padding to an ImageButton in Android
- How to set an ImageButton background transparent in Android