Question

I'm new to Android application development. I would like to ask the use of image button in Android programming when simple button can also add the image with the button. How can we generate click event of image button?

Was it helpful?

Solution

If you will look in the API for Button you will see that it has a method called setOnClickListener that is inherited from View. Since ImageButton is also a view, you can also call the same method for it.

The only way that I see to use an image in a Button is by using android:background in the XML. This is only used for setting what's shown behind the text of a button. You should use ImageButton when you want to make a button that only uses the image as its defining feature. If you end up wanting to only see the image and have no part of the button background visible, you can set android:background on the button to use an invisible Drawable.

OTHER TIPS


The Image Button control is a special type of button that displays a Drawable graphic instead of text.

The Image Button and Button controls are both derived from the View class, but they are unrelated to each other. The Button class is actually a direct subclass of Text View (think of it as a line of text with a background graphic that looks like a button), whereas the Image Button class is a direct subclass of Image View.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top