How to Enable/Disable Click Listener on Views in Android?
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 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:
Please Login to comment...