Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Enable/Disable Click Listener on Views in Android?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Sometimes, when we come across a situation where we need not want to click on some clickable views. Like if mandatory data is not filled in by the user, So in that situation it is very helpful to have a clicks control over our views. So in this article, we will learn how to disable/enable click listener on views in android.

 Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />
        app:layout_constraintVertical_bias="0.303" />
        app:layout_constraintTop_toBottomOf="@+id/tvMain" />

Note: We have also included vector images in the drawable folder, if want to use ImageView you also need to add a vector image.

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


package com.ayush.gfg_exit
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    lateinit var tvMain : TextView
    lateinit var btnView : Button
    override fun onCreate(savedInstanceState: Bundle?) {
        tvMain = findViewById(
        btnView = findViewById(
        btnView.setOnClickListener {
        // btnView is initially enabled(true).
        // clicking tvMain each time, 
          // btnView.isEnalbed will toggles.
        tvMain.setOnClickListener {
            btnView.isEnabled = !(btnView.isEnabled)


My Personal Notes arrow_drop_up
Last Updated : 01 Feb, 2022
Like Article
Save Article
Similar Reads