ImageButton In Android Studio With Example

ImageButton In Android – Introduction

ImageButton In Android –

Create activity_button_Image File in XML

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
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:layout_marginTop=”20dp”
android:orientation=”vertical”
android:layout_gravity=”center”
tools:context=”.buttonImageActivity”>

<ImageButton
android:id=”@+id/imageButton1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:src=”@mipmap/ic_launcher_round”/>

<ImageButton
android:id=”@+id/imageButton2″
android:layout_width=”250dp”
android:layout_height=”250dp”
android:layout_marginTop=”20dp”
android:layout_gravity=”center”
android:src=”@mipmap/ic_launcher_round”/>

<ImageButton
android:id=”@+id/imageButton3″
android:layout_width=”match_parent”
android:layout_height=”250dp”
android:layout_marginTop=”20dp”
android:layout_gravity=”center”
android:src=”@mipmap/ic_launcher_round”/>

</LinearLayout>

Create buttonImageActivity File in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class buttonImageActivity extends AppCompatActivity {

ImageButton imageButton1, imageButton2, imageButton3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_image);

imageButton1 = findViewById(R.id.imageButton1);
imageButton2 = findViewById(R.id.imageButton2);
imageButton3 = findViewById(R.id.imageButton3);

imageButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonImageActivity.this, “First Image Clicked”, Toast.LENGTH_SHORT).show();
}
});

imageButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonImageActivity.this, “Second Image Clicked”, Toast.LENGTH_SHORT).show();
}
});

imageButton3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(buttonImageActivity.this, “Third Image Clicked”, Toast.LENGTH_SHORT).show();
}
});
}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.buttonImageActivity” />
</application>

</manifest>

ImageButton In Android – Output

Check Out the Headphones – 50% to 70% Off
Check Out the Earbuds – 50% to 70% Off
Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Check Out The LAB Configuration eBook – Class A, Class B, Class C, MiN worth $78 Get at $18

Leave a Reply

Your email address will not be published.