Open In App
Related Articles

Dynamic ImageButton in Kotlin

Improve Article
Save Article
Like Article

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.

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.

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.


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

Modify the strings.xml file to write the name of the application


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

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(
  // Adding ImageButton in LinearLayout

Other process similar to manually adding the Image Button in the layout.


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val num1 = findViewById<EditText>(
        val num2 = findViewById<EditText>(
        val imgBtn = ImageButton(this)
        imgBtn.layoutParams = LinearLayout.LayoutParams(
        val linearLayout = findViewById<LinearLayout>(
        // Adding ImageButton in LinearLayout
        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())
                    "Multiplication 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:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Mar, 2022
Like Article
Save Article
Similar Reads