Hello Friends,
In this tutorial I am showing you how to bind the click listener on View in Kotlin.
1. Code snippet to set OnClickListener on Textview in Kotlin Android
2. Code snippet to set OnClickListener on Button in Kotlin Android
3. Code snippet to set OnClickListener on ImageView in Kotlin Android
Hope this will help someone.
Enjoy Coding..... :)
In this tutorial I am showing you how to bind the click listener on View in Kotlin.
1. Code snippet to set OnClickListener on Textview in Kotlin Android
val tvStatic = findViewById(R.id.tv_static) as TextView tvStatic.setOnClickListener { // your code to perform when the user clicks on the TextView Toast.makeText(this, "You clicked on TextView 'Click Me'.", Toast.LENGTH_SHORT).show() }
2. Code snippet to set OnClickListener on Button in Kotlin Android
// get reference to button val btn_click = findViewById(R.id.btn_click_me) as Button // set on-click listener btn_click.setOnClickListener { Toast.makeText(this@MainActivity, "You clicked on Button.", Toast.LENGTH_SHORT).show() }
3. Code snippet to set OnClickListener on ImageView in Kotlin Android
// get reference to ImageView val iv_profile = findViewById(R.id.image_view) as ImageView // set on-click listener for ImageView iv_profile.setOnClickListener { // your code here }
Hope this will help someone.
Enjoy Coding..... :)