Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.
We can add an image to the button simply by using
In android, we can create ImageButton control in two ways either manually or programmatically.
First we create a new project by following the below steps:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
In this article, we will create the ImageButton programmatically in Kotlin file.
Use LinearLayout in activity_main.xml file
In this file, we will add only the EditText and set attributes for both of them to access into the Kotlin file.
Modify the strings.xml file to write the name of the application
Create ImageButton in MainActivity.kt file
We will declare a variable imgBtn to create ImageButton.
val imgBtn = ImageButton(this)
then, set the image resource for the button using
In the end, add the button into LinearLayout using
val linearLayout = findViewById
(R.id.linear_layout) // Adding ImageButton in LinearLayout linearLayout.addView(imgBtn)
Other process similar to manually adding the Image Button in the layout.
Run as Emulator:
- ImageButton in Kotlin
- Dynamic RadioGroup in Kotlin
- Dynamic AutoCompleteTextView in Kotlin
- Dynamic ImageView in Kotlin
- Dynamic DatePicker in Kotlin
- Dynamic ScrollView in Kotlin
- Dynamic Button in Kotlin
- Dynamic TimePicker in Kotlin
- Dynamic TextView in Kotlin
- Dynamic EditText in Kotlin
- Dynamic ProgressBar in Kotlin
- Dynamic RadioButton in Kotlin
- Dynamic SeekBar in Kotlin
- Dynamic TextSwitcher in Kotlin
- Dynamic Switch in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.