The android.widget.ImageButton is a class available in the Android SDK that allows developers to add clickable images to their user interface. This widget inherits from the ImageView class, so it can display images in various formats, including JPEG, PNG, BMP, and GIF.
To use ImageButton in your app, you need to import the following package:
import android.widget.ImageButton;
Once you've done that, you can start creating ImageButton objects and adding them to your UI. Here is an example code snippet that demonstrates how to create and set an image for an ImageButton in a layout:
In this example, we've created an ImageButton with an ID "myImageButton" and set its image source to "my_icon". We've also set the background to null so that the button doesn't have any additional styling.
Another example of an ImageButton in action is when it is used as a button that performs a specific action. Here is an example code snippet that demonstrates how to create an ImageButton that switches between two different images when the button is pressed:
@Override public void onClick(View v) { if (isOn) { myButton.setImageResource(R.drawable.off_icon); isOn = false; } else { myButton.setImageResource(R.drawable.on_icon); isOn = true; } } });
In this example, we've created an ImageButton object with ID "myImageButton" and added an OnClickListener that toggles between two different images when the button is pressed. When the button is first pressed, the isOn variable is set to false and the button switches to the "off_icon" image. When pressed again, the isOn variable is set back to true and the button switches to the "on_icon" image.
The package library for android.widget is "android.widget".
Java ImageButton - 30 examples found. These are the top rated real world Java examples of android.widget.ImageButton extracted from open source projects. You can rate examples to help us improve the quality of examples.