Quick Contact


    Android EditText with TextWatcher

    TextView is a subclass of Android EditText. EditText is a text editor that allows you to enter and alter text. When utilising the width of EditText, we must provide the input type in the inputType property of EditText, which configures the keyboard based on input.

    To keep track of changes made in EditText, the TextWatcher interface is used. EditText does this by using the addTextChangedListener() function.

    If an application has a login form that the user must fill out, the login button should be hidden (that is, it should not be clickable). When the user enters the form’s credentials, the button should be enabled for the user to click. As a result, in this post, we’ll add a TextWatcher to the EditText box. Take a look at the graphic below to get a better sense of what the TextWatcher is and how it may improve user interaction. It’s worth noting that we’ll be utilising the Java programming language to complete this project.

    activity_main.xml

    < ?xml version="1.0" encoding="utf-8"?>

    < RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools=”http://schemas.android.com/tools”

    android:layout_width=”match_parent”

    android:layout_height=”match_parent”

    android:paddingBottom=”@dimen/activity_vertical_margin”

    android:paddingLeft=”@dimen/activity_horizontal_margin”

    android:paddingRight=”@dimen/activity_horizontal_margin”

    android:paddingTop=”@dimen/activity_vertical_margin”

    tools:context=”com.example.test.searchfromlistview.MainActivity”>

    < EditText

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/editText”

    android:inputType=”text”

    android:layout_alignParentTop=”true”

    android:layout_alignParentLeft=”true”

    android:layout_alignParentStart=”true”

    android:layout_alignParentRight=”true”

    android:layout_alignParentEnd=”true” />

    < ListView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/listView”

    android:layout_below=”@+id/editText”

    android:layout_alignParentLeft=”true”

    android:layout_alignParentStart=”true” />

    < ContentView

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/editText”

    android:inputType=”text”

    android:layout_alignParentTop=”true”

    android:layout_alignParentLeft=”true”

    android:layout_alignParentStart=”true”

    android:layout_alignParentRight=”true”

    android:layout_alignParentEnd=”true” />

    < ListView1

    android:layout_width=”wrap_content”

    android:layout_height=”wrap_content”

    android:id=”@+id/listView”

    android:layout_below=”@+id/editText”

    android:layout_alignParentLeft=”true”

    android:layout_alignParentStart=”true” />

    < /RelativeLayout>

    Create a new file in the layout folder called list item.xml that includes the data for the ListView.

    list_item.xml

    < ?xml version="1.0" encoding="utf-8"?>

    < LinearLayout

    xmlns:android=”http://schemas.android.com/apk/res/android”

    android:orientation=”vertical”

    android:layout_width=”match_parent”

    android:layout_height=”match_parent”>

    < TextViewandroid:id="@+id/product_name"

    android:layout_width=”fill_parent”

    android:layout_height=”wrap_content”

    android:padding=”41dip”

    android:textSize=”11dip”

    android:textStyle=”bold”/>

    < /LinearLayout>

    Activity class

    package com.example.test.searchfromlistview;

    import android.os.Bundle;

    import android.text.Editable;

    import android.text.TextWatcher;

    import android.widget.ArrayAdapter;

    import android.widget.EditText;

    import android.widget.ListView;

    import android.support.v7.app.AppCompatActivity;

    import android.widget.Toast;

    	public class MainActivity extends AppCompatActivity {  
    
        private ListView lv;  
        private EditTexteditText;  
        private ArrayAdapter< String> adapter;  
    
        private String products[] = {"H", "K","S", "N", "Y", "N","M", "K",};  
    	    @Override  
    	    protected void onCreate(Bundle savedInstanceState) {  
    	super.onCreate(savedInstanceState);  
    	setContentView(R.layout.activity_main);  
    
    	        lv = (ListView) findViewById(R.id.listView);  
    	editText = (EditText) findViewById(R.id.editText);  
    	        adapter = new ArrayAdapter< String>(this, R.layout.list_item, R.id.product_name, products);  
    	lv.setAdapter(adapter);  
    
    	editText.addTextChangedListener(new TextWatcher() {  
    
    	            @Override  
    	            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {  
    	adapter.getFilter().filter(cs);  
    	            }  
    
    	            @Override  
    	            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {  
    	Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();  
    	            }  
    
    	            @Override  
    	            public void afterTextChanged(Editable arg0) {  
    	Toast.makeText(getApplicationContext(),"after text change",Toast.LENGTH_LONG).show();  
    	            }  
    	        });  
    	    }  
    	}
        

    Must Read About Android Tutorial.

    Example 2: –
        < ?xml version="1.0" encoding="utf-8"?>
    	< RelativeLayout
    		xmlns:android="http://schemas.android.com/apk/res/android"
    		xmlns:tools="http://schemas.android.com/tools"
    		android:layout_width="match_parent"
    		android:layout_height="match_parent"
    		tools:context=".MainActivity"
    		tools:ignore="HardcodedText">
    
    		< EditText
    			android:id="@+id/etEmail"
    			android:layout_width="match_parent"
    			android:layout_height="wrap_content"
    			android:layout_marginStart="16dp"
    			android:layout_marginTop="64dp"
    			android:layout_marginEnd="16dp"
    			android:hint="Email"
    			android:inputType="textEmailAddress" />
    
    		< EditText
    			android:id="@+id/etPassword"
    			android:layout_width="match_parent"
    			android:layout_height="wrap_content"
    			android:layout_below="@id/etEmail"
    			android:layout_marginStart="141dp"
    			android:layout_marginEnd="21dp"
    			android:hint="Password"
    			android:inputType="textPassword" />
    
    		< Button1
    			android:id="@+id/loginButton"
    			android:layout_width="wrap_content"
    			android:layout_height="wrap_content"
    			android:layout_below="@id/etPassword"
    			android:layout_centerHorizontal="true"
    			android:layout_marginTop="160dp"
    			android:enabled="false"
    			android:text="LOGIN" />
    
    	< EditText
    			android:id="@+id/etEmail"
    			android:layout_width="match_parent"
    			android:layout_height="wrap_content"
    			android:layout_marginStart="54dp"
    			android:layout_marginTop="21dp"
    			android:layout_marginEnd="16dp"
    			android:hint="Email"
    			android:inputType="textEmailAddress" />
    
    		< EditText
    			android:id="@+id/etPassword"
    			android:layout_width="match_parent"
    			android:layout_height="wrap_content"
    			android:layout_below="@id/etEmail"
    			android:layout_marginStart="61dp"
    			android:layout_marginEnd="25dp"
    			android:hint="Password"
    			android:inputType="textPassword" />
    
    		< Button2
    			android:id="@+id/loginButton"
    			android:layout_width="wrap_content"
    			android:layout_height="wrap_content"
    			android:layout_below="@id/etPassword"
    			android:layout_centerHorizontal="true"
    			android:layout_marginTop="16dp"
    			android:enabled="false"
    			android:text="logout" />
    	< /RelativeLayout>
        

    Both EditTexts can be handled independently as well. However, in this situation, the callback listener TextWatcher is built, and the callback listener object is provided to the addTextChangedListener function for each edit text to decrease the lines of code.

    In the MainActivity, run the following code.

    For better comprehension, comments have been added to the java files.

        import androidx.appcompat.app.AppCompatActivity;
    	import android.os.Bundle;
    	import android.text.Editable;
    	import android.text.TextWatcher;
    	import android.widget.Button;
    	import android.widget.EditText;
    
    	public class MainActivity extends AppCompatActivity {
    
    		
    		EditTextetEmail, etPassword;
    
    
    		Button bLogin;
    
    		
    		private TextWatchertextWatcher = new TextWatcher() {
    			@Override
    			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    			}
    
    			@Override
    			public void onTextChanged(CharSequence s, int start, int before, int count) {
    				
    				String emailInput = etEmail.getText().toString();
    				String passwordInput = etPassword.getText().toString();
    
    				bLogin.setEnabled(!emailInput.isEmpty() && !passwordInput.isEmpty());
    			}
    
    			@Override
    			public void afterTextChanged(Editable s) {
    
    			}
    		};
    
    		@Override
    		protected void onCreate(Bundle savedInstanceState) {
    			super.onCreate(savedInstanceState);
    			setContentView(R.layout.activity_main);
    
    			
    			etEmail = findViewById(R.id.etEmail);
    			etPassword = findViewById(R.id.etPassword);
    			bLogin = findViewById(R.id.loginButton);
    
    			etEmail.addTextChangedListener(textWatcher);
    			etPassword.addTextChangedListener(textWatcher);
    
    
    		protected void onCreate(Bundle savedInstanceState) {
    			super.onCreate(savedInstanceState);
    			setContentView(R.layout.activity_main);
    
    			
    			etEmail = findViewById(R.id.etEmail);
    			etPassword = findViewById(R.id.etPassword);
    			bLogin = findViewById(R.id.loginButton);
    
    			etEmail.addTextChangedListener(textWatcher);
    			etPassword.addTextChangedListener(textWatcher);
    		}
    	}
        
    Output:
    Android EditText with TextWatcher

    Enroll Yourself in Live Classes For Android training in delhi.

    Copyright 1999- Ducat Creative, All rights reserved.