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 ImageView, a click on the ImageView triggers the action and code inside setOnClickListener method is executed.
In this tutorial, we will learn how to set OnClickListener for ImageView in Kotlin file, with the help of an example.
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
}
Example – OnClickListener for ImageView
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.
Following are the screenshots to demonstrate the setOnClickListener for ImageView :
Conclusion
In this Kotlin Android Tutorial, we have learnt how to set on-click listener for ImageView using TextView.setOnClickListener() method.