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="#F8F8FF"
android:padding="16dp"
tools:context=".MainActivity">
<!-- ImageButton no padding by XML -->
<ImageButton
android:id="@+id/ib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:padding="0dp"
android:scaleType="fitCenter"
android:src="@drawable/share_stroke"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- ImageButton 16dp padding by XML -->
<ImageButton
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:padding="16dp"
android:src="@drawable/share_stroke"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ib" />
<!-- ImageButton padding programmatically -->
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/share_stroke"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ib2" />
<!-- ImageButton with default padding -->
<ImageButton
android:id="@+id/ib4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/share_stroke"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ib3" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.cfsuman.androidtutorials;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
ImageButton ib3 = findViewById(R.id.ib3);
// Set padding for third ImageButton
// Set various pixels padding for ImageButton left,
// top, right and bottom side
ib3.setPadding(60,50,40,30);
}
}

- How to create a DatePicker without calendar 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 add a border to an ImageButton in Android
- How to use ImageButton different image ScaleType in Android
- How to resize/scale an ImageButton in Android
- How to set an ImageButton background transparent in Android
- How to add listener for a ToggleButton in Android
- How to change ToggleButton on/off text in Android
- Android Switch Button Example
- How to create an AlertDialog with custom layout/view in Android
- How to add a cancel button on AlertDialog in Android
- How to use AlertDialog onClickListener in Android
- How to set prompt text in a Spinner in Android