How to Enable/Disable Click Listener on Views in Android? Last Updated : 01 Feb, 2022 Comments Improve Suggest changes Like Article Like Report 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 <?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:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/ic_geeksforgeeks" app:layout_constraintBottom_toTopOf="@+id/tvMain" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enable/Disable" android:textColor="#7CB342" android:textSize="20dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.303" /> <Button android:id="@+id/btnMain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvMain" /> </androidx.constraintlayout.widget.ConstraintLayout> 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. Kotlin package com.ayush.gfg_exit import androidx.appcompat.app.AppCompatActivity 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?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) tvMain = findViewById(R.id.tvMain) btnView = findViewById(R.id.btnMain) btnView.setOnClickListener { Toast.makeText(this,"clicked",Toast.LENGTH_SHORT).show() } // btnView is initially enabled(true). // clicking tvMain each time, // btnView.isEnalbed will toggles. tvMain.setOnClickListener { btnView.isEnabled = !(btnView.isEnabled) } } } Output: Comment More infoAdvertise with us Next Article How to Enable/Disable Click Listener on Views in Android? A ayushpandey3july Follow Improve Article Tags : Kotlin Android Similar Reads How to Enable/Disable Button in Android? The Enable/Disable Feature is used in many Android apps so the basic use of that feature is to prevent the user from clicking on a button until they will add all the required fields that have been asked. We are considering an example of email and password if the user has entered the email and passwo 3 min read How to Draw Border on Event of Click in Android with Kotlin? Sometimes we want to display to the users that they have clicked an item by showing a border on the item. So, for this, we need to create two drawable resource files, one for the normal background of the item and another for the border. A sample video is given below to get an idea about what we are 3 min read How to Handle OnClick Listener using When keyword in Android? In this article, we are going to see how can we apply the OnClick event to a button in android with the help of When Keyword, Â we can also apply the onClick event to a button in various other ways such as by setOnClickListener and findViewById, by ViewBinding but all these methods are taken a long t 4 min read How to Disable GridView Scrolling in Android? A GridView is a ViewGroup that can display data from a list of objects or databases in a grid-like structure consisting of rows and columns. Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridVi 4 min read How to Apply One Listener to Multiple Buttons in Android? In this article we are going to write a shortcode for applying click events over different buttons, rather than writing different methods for different buttons, we are going to build only a single method that is onClick() for all buttons present and by using the concept of switch case we can perform 3 min read How to Implement View Binding Inside Dialog in Android? In android, View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views th 4 min read How to Disable RecyclerView Scrolling in Android? RecyclerView is a view group used for displaying data from arrays and databases. RecyclerView basically is a list of items from the data. RecyclerView is often referred to as a successor of GridView and ListView. More about RecyclerView could be found at RecyclerView in Android with Example. Recycle 3 min read How to Make Substring of a TextView Clickable in Android? In this article, we are going to implement a very important feature related to TextView. Here we are making part of a string or a substring to act as a substring. This feature is important while writing a blog because it may be possible that for certain points we want to redirect the user to a link. 2 min read How to Disable Back Press Button in Android? Physical keys on Android let users lock the phone, switch off the device, control volume, go back, go home and display background applications. Between all the keys, the back button is used the most in applications for navigating between activities. However, if the navigation stage reaches the start 2 min read How to Set Calendar Event in Android? In most Android devices, a Calendar is an application that displays a calendar and is most commonly used to set reminders or events. Calendar applications in general are very light-weighted and consume less memory. Calendar has a background thread running that keeps a track of when to alert the user 3 min read Like