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 password then only the user can click the button. If anyone of the edittext-field is empty then the user can’t able to click on the button. A sample video is given below to get an idea about what we are going to do in this article.
Step-by-Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
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. We have created one simple activity_main.xml file with two EditText fields and one Button. We have used CardView inside if you already created that then you can move to the next step.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout android:layout_width = "match_parent" android:background = "@color/black" android:layout_height = "match_parent" tools:context = ".MainActivity" > < androidx.cardview.widget.CardView android:id = "@+id/login_card" android:layout_width = "0dp" android:layout_height = "wrap_content" android:layout_margin = "16dp" android:layout_marginStart = "16dp" android:layout_marginEnd = "16dp" app:cardCornerRadius = "16dp" app:cardBackgroundColor = "#EAF2F8" app:cardUseCompatPadding = "true" app:cardElevation = "10dp" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.0" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" app:layout_constraintVertical_bias = "0.439" > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" android:padding = "16dp" > < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "Geeks For Geeks" android:gravity = "center" android:textColor = "#00FF00" android:textSize = "24sp" android:textStyle = "bold" /> < EditText android:id = "@+id/etEmail" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:drawableStart = "@drawable/baseline_email_24" android:layout_marginTop = "16dp" android:background = "@drawable/edittext_background" android:hint = "Email" android:inputType = "textEmailAddress" android:padding = "8dp" android:textColor = "#222222" android:textSize = "16sp" /> < EditText android:id = "@+id/PassEt" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:drawableStart = "@drawable/baseline_lock_24" android:background = "@drawable/edittext_background" android:hint = "Password" android:inputType = "textPassword" android:padding = "8dp" android:textColor = "#222222" android:textSize = "16sp" /> < Button android:id = "@+id/button" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "16dp" android:textColor = "#2C3E50" android:background = "@drawable/edittext_background" android:text = "Log In" android:textSize = "16sp" /> </ LinearLayout > </ androidx.cardview.widget.CardView > </ androidx.constraintlayout.widget.ConstraintLayout > |
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail. We have added one Toast if the user will click the button then it’ll show one Toast Message.
Java
package com.android.gfgapp; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText etEmail, PassEt; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); etEmail = findViewById(R.id.etEmail); PassEt = findViewById(R.id.PassEt); button = findViewById(R.id.button); // when app opens and user didn't added // email and password then user can't click button.setEnabled( false ); etEmail.addTextChangedListener(LoginWatcher); PassEt.addTextChangedListener(LoginWatcher); // added one Toast to let you know that // the button is been clicked or not button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity. this , "Clicked Button" , Toast.LENGTH_SHORT).show(); } }); } TextWatcher LoginWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { String userName = etEmail.getText().toString(); String userPass = PassEt.getText().toString(); button.setEnabled(!userName.isEmpty() && !userPass.isEmpty()); } @Override public void afterTextChanged(Editable editable) { } }; } |
Output: