Android ImageButton Using Kotlin With Example

We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at

Hello Readers! In this post, we are going to learn about android imageButton using kotlin in any android application. We will also learn about different attributes of android imageButton that can be used to customise this widget.


Tutorialwing Kotlin ImageButton Output Android ImageButton Using Kotlin

Tutorialwing Kotlin ImageButton Output

Getting Started

Android ImageButton can be defined as below –

ImageButton is a subclass of imageView that displays a button with image that can be clicked or pressed by user.

By default, it acts as button. However, you can set image using android:src attribute.

Different Attributes of Android imageButton Widget

Some of the popular attributes of imageButton inherited from imageView are –

Sr. XML Attributes Description
1 android:adjustViewBounds Responsible for auto adjust of view’s boundary to maintain the aspect ratio of it’s drawable.
2 android:baseline Defines offset of the baseline within this view.
3 android:baselineAlignBottom If true, it sets the baseline aligned with it’s bottom edge.
4 android:cropToPadding If true, image will be cropped to fit within it’s padding.
5 android:maxHeight Defines maximum height of the view.
6 android:maxWidth Defines maximum width of the view.
7 android:scaleType Specifies the way image will be resized or moved to match the size of the imageView
8 android:src Defines a drawable of the imageView
9 android:tint It tints the color of the image.
10 android:tintMode Specifies blending mode used to apply the image tint

Some of the popular attributes of ImageButton inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines alpha of the view
2 android:autofillHints Defines the data to be shown to auto fill in the view
3 android:background Defines background of the view
4 android:backgroundTint Defines tint to apply to the background
5 android:backgroundTintMode Specifies Blending mode used to apply the background tint
6 android:clickable Defines whether this view is clickable or not
7 android:elevation Specifies elevation to the view
8 android:fitsSystemWindows It is used to adjust view layout based on system windows
9 android:focusable Defines whether this view can take focus
10 android:id Defines unique id to the view
11 android:onClick Specifies what to do when this view is clicked
12 android:padding Defines padding to the view
13 android:tag Defines tag to the view
14 android:tooltipText Specifies tooltip text to be shown when hovered on the view
15 android:visibility Defines visibility of the view

Example of Android ImageButton Using Kotlin

At first, we will create android application. Then, we will use imageButton using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ImageButton. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use imageButton using kotlin in the application.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">ImageButton</string>
	<string name="this_is_imagebutton">This is imageButton</string>
	<string name="image_button_clicked">You clicked imageButton</string>

3. Use ImageButton Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>



In activity_main.xml file, we have defined imageButton widget. Notice the two attributes android:contentDescription and app:srcCompat . First one is being used to set an description to the image that will be shown when there is no image found. Second one is used to set image resource to the imageButton. Now, we will access this imageButton using kotlin file in the application.

4. Access ImageButton Widget in Kotlin file

Open src/main/java/com.tutorialwing.imagebutton/MainActivity.kt file and add below code into it.

package com.tutorialwing.imagebutton

import android.os.Bundle
import android.widget.ImageButton
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val imageButton = findViewById<ImageButton>(
        imageButton?.setOnClickListener { Toast.makeText(this@MainActivity, R.string.image_button_clicked, Toast.LENGTH_SHORT).show() }

In MainActivity.kt file, we have accessed ImageButton widget. Then, we have set a click listener to it. On click, it will display a toast message.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.imagebutton"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android imageButton using kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners