Home »
Android
ImageButton in Android with Example
Android GridView: In this tutorial, we will learn about the implementation of GridView with the help of an example and codes in Android.
By Manu Jemini Last updated : June 06, 2023
What is ImageButton in Android?
ImageButton is a very easy way to have a function of the button, but also using a beautiful image. ImageButton provides very simple techniques to set it. For this tutorial to work, just create a new project with any name. You will get a default activity namely ‘MainActivity’, you can make use of it.
How to Use ImageButton in Android?
Starting with your XML file, drag and drop it from the widget section. There is not implication about the parent layout type. Make your layout the way you want it. You should set the src of ImageButton.
The second thing is your JAVA file. To begin with, you should import ImageButton from the widget, this is done by adding this line import android.widget.ImageButton; After that, in your activity, there would a default onCreate() function, this would be pre-set to use quickly.
Now, you would need to have a variable of type ImageButton and set it with the reference of the ImageButton in the layout file.
Now, you would need to have a variable of type TextView and set it with the reference of the TextView in the layout file.
Above Lines would do it. The Purpose is to change the content of TextView on the click on ImageButton. All this can be done by an event click Listener. Check the Full Code Below.
After doing everything up, your code should look like this. Connect an Android device or run an ADV and then build your project to see the result.
Android ImageButton Example with Code Files
Java file
package com.example.hp.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ImageButton button;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(ImageButton)findViewById(R.id.button);
result=(TextView)findViewById(R.id.result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("Image button has been pressed");
}
});
}
}
XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:src="@drawable/button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result"
android:layout_weight="0.19"
android:hint="Result" />
</LinearLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.demo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output