Home Tutorials Hints & Tips

Getting Started in Android Development

Part 17 - The Image Button

The next three tutorials look at how we can construct a springboard menu using image buttons and the GridLayout. The focus of this tutorial is how to use the image buttons.

The use of images is a fundamental part of any app. An app that is graphically appealing will be far more successful than one based on non-graphical views alone.

As the name implies, an image button is a view that has the function of a standard button, but uses an image as the background. Like the standard button, you can assign an “onClick” Listener to the button in which you place the code with the button action.

The application of the code in the XML layout is similar to any other view. For this tutorial, four images buttons are placed in a linear layout. The images used can be downloaded here.

Download Download example image buttons

The code defining a typical image button is shown below:

<ImageButton
android:id="@+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image1" />

The android:background=”@drawable/<image_file>” attribute points to the image in the drawable folder. Remember that you will need an image for each screen density in the respective drawable folder. That is all there is to the image button XML code.

After this tutorial the activity_gridlayout.xml layout should look similar to the one below:

Turning our attention to the java code, there is really nothing new compared to what we have undertaken in the previous tutorials. An onClick listener is setup for each button and the toast is generated for each button. At the end of this tutorial your GridLayoutActivity.java file should look similar to the one below:

After this tutorial your Android Manifest file should look similar to the one below:

After this tutorial your ListviewActivity.java file should look similar to the one below:


Download Download tutorial set 1

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved