Skip to content
Related Articles

Related Articles

Improve Article

ImageButton in Kotlin

  • Last Updated : 19 Feb, 2021

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 attribute android:src in activity_main.xml file or by using setImageResource() method.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Different attributes of Switch widget

XML AttributesDescription
android:idUsed to uniquely identify the control.
android:srcUsed to specify the source file of the image.
android:onClickUsed to Specifies what to do when this button is clicked.
android:visibilityUsed to set visibility of the image button.
android:backgroundUsed to set background color of the Image button.
android:maxHeightUsed to set maximum height of the Image button view.
android:maxWidthUsed to set maximum width of the Image button view.
android:paddingUsed to set the padding from left, right, top and bottom.

Use ImageBotton in activity_main.xml file

In this file, we include the Edittext and ImageButton and set their attributes like id, layout_width, hint etc.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
        android:hint= "Enter first number"/>
        android:hint= "Enter second number"/>
        android:src="@android:drawable/btn_plus" />

Modify the strings.xml file to add application name

    <string name="app_name">ImageButtonInKotlin</string>

Access ImageButton and EditText in MainActivity.kt file

First of all, we declare two variables num1 and num2 for both edittext and access them using the ids.

val num1 = findViewById(
val num2 = findViewById(

then, we declare variable imgbtn for the ImageButton and set the OnCLickListener to check the filled is empty or not

val imgbtn = findViewById(
    imgbtn.setOnClickListener {
     if (num1.text.toString().isEmpty() || num2.text.toString().isEmpty()) {
           "Enter both numbers", Toast.LENGTH_SHORT).show()

package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val num1 = findViewById<EditText>(
        val num2 = findViewById<EditText>(
        val imgbtn = findViewById<ImageButton>(
        imgbtn.setOnClickListener {
         if (num1.text.toString().isEmpty() || num2.text.toString().isEmpty()) {
                  "Enter both numbers", Toast.LENGTH_SHORT).show()
            else {
                val num1 = Integer.parseInt(num1.text.toString())
                val num2 = Integer.parseInt(num2.text.toString())
                    "Sum of the numbers = " + (num1 + num2),

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:


My Personal Notes arrow_drop_up
Recommended Articles
Page :