In Android, ImageView is a child class of View, and hence the method setOnClickListener() could be used on the object of type ImageView.
When OnClickListener is set to an ImageView, a click on the image triggers the action written inside the listener. This is useful when an image has to behave like a button, such as a profile icon, close icon, favorite icon, menu image, or custom image button.
In this tutorial, we will learn how to set OnClickListener for ImageView in a Kotlin Android file, with a working example. We will also cover common reasons why an ImageView click may not work.
Set OnClickListener for ImageView in Kotlin Android
The basic idea is simple. First get the reference of the ImageView from the layout using its id. Then call setOnClickListener { } on that ImageView. The Kotlin lambda passed to setOnClickListener runs whenever the user taps the image.
Code – ImageView OnClickListener
Following is quick look into code to set OnClickListener for ImageView in Kotlin Android :
// get reference to ImageView
val image_view = findViewById(R.id.image_view) as ImageView
// set on-click listener for ImageView
image_view.setOnClickListener {
// your code here
}
In current Kotlin Android projects, you may also write the same statement with a typed findViewById call.
val imageView = findViewById<ImageView>(R.id.iv_click_me)
imageView.setOnClickListener {
Toast.makeText(this, "ImageView clicked", Toast.LENGTH_SHORT).show()
}
Example – OnClickListener for ImageView in Kotlin Activity
In this example, we shall look into the layout xml file and Activity(Kotlin file) to set OnClickListener for a ImageView.
Create an Android Application with Kotlin Support and replace activity_main.xml and MainActivity.kt with the following content.
activity_main.xml
We have a ImageView in LinearLayout. We will use this ImageView to set on-click listener.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutorialkart.myapplication.MainActivity">
<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_click_me"
android:src="@drawable/image1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.kt
Get reference to the the ImageView in layout file using id, and then call setOnClickListener {} on this ImageView. When user clicks on this ImageView, the code inside setOnClickListener {} will be executed.
package com.tutorialkart.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
/**
* An example to set OnClickListener for ImageView in Kotlin Android
*/
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get reference to ImageView
val iv_click_me = findViewById(R.id.iv_click_me) as ImageView
// set on-click listener
iv_click_me.setOnClickListener {
// your code to perform when the user clicks on the ImageView
Toast.makeText(this@MainActivity, "You clicked on ImageView.", Toast.LENGTH_SHORT).show()
}
}
}
Keep an image in the folder app/res/drawabale/ with name image1.png. This image has been given as the src (source) to ImageView in the activity_main.xml layout file.
If you are creating a new Android project with AndroidX, the layout and Activity imports may look slightly different. The listener concept remains the same.
AndroidX ImageView OnClickListener Kotlin Example
The following AndroidX example uses androidx.constraintlayout.widget.ConstraintLayout and androidx.appcompat.app.AppCompatActivity. Use this style in newer Android projects.
activity_main.xml – AndroidX layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv_click_me"
android:layout_width="120dp"
android:layout_height="120dp"
android:contentDescription="Clickable sample image"
android:src="@drawable/image1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt – AndroidX Activity
package com.tutorialkart.myapplication
import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val imageView = findViewById<ImageView>(R.id.iv_click_me)
imageView.setOnClickListener {
Toast.makeText(this, "You clicked on ImageView.", Toast.LENGTH_SHORT).show()
}
}
}
Use ImageView as a Button in Kotlin Android
An ImageView can be used as a simple clickable image, but it does not automatically provide all button behavior. If the image is intended to work like a button, add a proper contentDescription, give it a visible size, and make the clickable area large enough for touch input.
You can also make the clickable behavior clear in XML by setting android:clickable and android:focusable. This is especially useful when you want the view to behave more like a control.
<ImageView
android:id="@+id/iv_favorite"
android:layout_width="48dp"
android:layout_height="48dp"
android:clickable="true"
android:focusable="true"
android:contentDescription="Add to favorites"
android:src="@drawable/ic_favorite" />
Then set the listener in Kotlin.
val favoriteImage = findViewById<ImageView>(R.id.iv_favorite)
favoriteImage.setOnClickListener {
Toast.makeText(this, "Favorite clicked", Toast.LENGTH_SHORT).show()
}
Change ImageView Image After Click in Kotlin
A common use case is changing the image after the user taps it. For example, you can switch between a favorite and non-favorite icon.
val favoriteImage = findViewById<ImageView>(R.id.iv_favorite)
var isFavorite = false
favoriteImage.setOnClickListener {
isFavorite = !isFavorite
if (isFavorite) {
favoriteImage.setImageResource(R.drawable.ic_favorite_filled)
} else {
favoriteImage.setImageResource(R.drawable.ic_favorite_outline)
}
}
Use image resource names that exist in your project under the res/drawable folder. If the drawable name is wrong, the project will not compile.
Set OnClickListener for ImageView Created Programmatically
If the ImageView is created in Kotlin instead of XML, you can still attach the click listener in the same way. Add the image view to a parent layout and then call setOnClickListener.
val imageView = ImageView(this).apply {
setImageResource(R.drawable.image1)
contentDescription = "Clickable image"
layoutParams = LinearLayout.LayoutParams(120, 120)
}
imageView.setOnClickListener {
Toast.makeText(this, "Dynamic ImageView clicked", Toast.LENGTH_SHORT).show()
}
val parentLayout = findViewById<LinearLayout>(R.id.ll_main_layout)
parentLayout.addView(imageView)
For a programmatically created ImageView, confirm that the parent layout exists in the current layout file and that the image view has proper width and height. A view with zero size cannot be clicked.
Why ImageView OnClickListener Does Not Work in Kotlin Android
If the ImageView click is not working, check the layout and Kotlin code together. Most issues come from an incorrect id, a transparent or zero-size view, another view placed above the image, or setting the listener before the correct layout is loaded.
| Problem | What to check | Fix |
|---|---|---|
| Wrong ImageView id | The id in XML and Kotlin must match. | Use the same id, for example R.id.iv_click_me. |
| Listener set before layout | findViewById must be called after setContentView. | Place listener code after setContentView(R.layout.activity_main). |
| ImageView has no visible size | The view may have zero width or height. | Use fixed dp size or valid constraints/layout params. |
| Another view is covering ImageView | A parent or sibling view may be placed above it. | Inspect the layout hierarchy and constraints. |
| Drawable is missing | The image resource may not exist. | Place the image in app/src/main/res/drawable with a valid lowercase resource name. |
| Click area is too small | wrap_content may create a small tappable area. | Use a larger width and height, such as 48dp or more for icon-like buttons. |
Following are the screenshots to demonstrate the setOnClickListener for ImageView :


Kotlin ImageView OnClickListener QA Checklist
- Does the tutorial show that
ImageViewinherits click handling from AndroidView? - Does the Kotlin code call
setOnClickListeneraftersetContentView? - Does the XML example include a valid
android:idused by the Kotlin file? - Does the tutorial explain how to use
ImageViewas a button withcontentDescription? - Does the troubleshooting section cover wrong id, missing drawable, layout overlap, and zero-size view issues?
- Are existing screenshots and links preserved without changing their URLs?
ImageView OnClickListener in Kotlin Android FAQs
How to set OnClickListener for ImageView in Kotlin Android?
Get the ImageView reference using findViewById<ImageView>(R.id.your_image_id) and call setOnClickListener { } on it. Write the code that should run after the click inside the lambda block.
Can ImageView be used as a button in Android?
Yes, an ImageView can be used as a clickable image button by setting an OnClickListener. For better usability, give it a clear size, add android:contentDescription, and make sure the touch area is large enough.
Why is ImageView OnClickListener not working?
Common reasons include a wrong view id, calling findViewById before setContentView, a missing drawable, another view covering the image, or an ImageView with no visible size. Check the XML layout and Kotlin code together.
How do I change an ImageView image after click in Kotlin?
Inside setOnClickListener, call setImageResource(R.drawable.your_drawable) on the ImageView. You can keep a Boolean variable if you want to toggle between two images.
Should I use ImageButton or ImageView with OnClickListener?
Use ImageButton when the control is clearly a button and you want button-like behavior by default. Use ImageView with setOnClickListener when you are displaying an image that also needs to respond to clicks.
Conclusion
In this Kotlin Android Tutorial, we have learnt how to set on-click listener for ImageView using the ImageView.setOnClickListener() method. We also covered AndroidX usage, changing the image after a click, creating an ImageView programmatically, and fixing common click listener issues.
TutorialKart.com