Android ImageButton Control

Android ImageButton is a button with an image, which a user clicks. A button with an image on the surface and an AbsoluteLayout is what it is know for.

Android ImageButton Control

Usage

Use ImageButton to give a new view to the button. For example, share button, start button, turn off button, play button, pause button, etc.

Example

Here’s a simple example showing the working on Android iMAGEButton on Android Studio. Here, we will be showing a message on ImageButton click.

If you’re beginning with Android, then learn how to run your first Android app in Android Studio.

Let’s start with the usage of Android ImageButton control.

MainActivity.java

The src\main\java\com\studyopedia\studyopediaapplication\MainActivity.java is a java class which sets the views. Here we will set the View to be displayed with the setContentView.

On button click, a toast will appear with a message “Studyopedia is a free learning website!” .Toast in Android is a notification message visible in the form of popup. We did the same while working with Android Button Control, but here we will be adding an ImageButton.

ImageButton adds an image to surface of a button.

activity_main.xml

For src\main\res\layout\activity_main.xml,
We’ll add our UI content here, 1 TextView and 1 ImageButton. Since, we have an ImageButton, so we will be adding an image in the src\main\res\drawable directory.

Here, we added our image submit.png to drawable,

Android ImageButton Drawable image

We will get this image in our code using the following attribute,

Now, we will add our code in activity_main.xml,

AndroidManifest.xml

The content of \src\main\AndroidManifest.xml, provides key information of your app to the system. The system needs this to run your code,

strings.xml

Here’s the code for res/values/strings.xml. The app name and any hardcoded strings will be visible. We hardcoded string “Studyopedia ImageButton Demo App” and used @string/title in activity_main.xml. Here, it gets mentioned under <resources>,

Output

We created an AVD, so now we will run our app using the same AVD. Go to Run > Run app or press the run button or Shift + F10.

This will install your app on the AVD and the following emulator with your app opens up,

Android ImageButton Control Output1

Click the ImageButton and now the toast message is visible,

Android ImageButton Control Output 2

 


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Android Button Control
Android ToggleButton Control
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading