Android EditText TextWatcher Example :
Android EditText TextWatcher are useful for getting EditText control value at the time of entering text in it by the user. This can serve many purposes. You can validate user input for certain validation. In this post we will see how to implement the same in our application.
To add a Textwatcher to your EditText control, you have add TextChangedListener listeners. Below is the sample code.
EditText edtView = (EditText) findViewById(R.id.YourEditTextIdHere); //Initialization of EditText //Adding the listeners edtView.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { txtView.setText("Total Characters entered : " + edtView.getText().length() + "."); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //Your code here } @Override public void afterTextChanged(Editable s) { //Your code here } });
Make a new application in Android studio with your desired package name and app name. Open the layout file activity_main.xml and make following changes to it.
res > layout > activity_main.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:background="#fff" android:padding="2dp" android:weightSum="10" > <EditText android:id="@+id/edtView" android:layout_width="match_parent" android:layout_height="0dp" android:background="@android:drawable/editbox_background_normal" android:textColor="#252525" android:gravity="top|left" android:hint="Enter your Text here." android:layout_weight="9" /> <TextView android:id="@+id/txtView" android:layout_width="match_parent" android:layout_height="0dp" android:textColor="#000" android:textStyle="bold" android:layout_weight="1" /> </LinearLayout>
This is a very basic layout wherein I have made a simple Android EditText control and TextView control.
The TextView control will be just displaying number of characters entered in the EditText control.
Now edit the java class, MainActivity.java as below :
MainActivity.java :
package parallelcodes.simplewidget; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { EditText edtView; TextView txtView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtView = (EditText) findViewById(R.id.edtView); txtView = (TextView) findViewById(R.id.txtView); edtView.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { txtView.setText("Total Characters entered : " + edtView.getText().length() + "."); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //Your code here } @Override public void afterTextChanged(Editable s) { //Your code here } }); } }
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="parallelcodes.simplewidget" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:windowSoftInputMode="adjustResize" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
When you run the application, TextView will be showing Number of characters entered in EditText control.
See also : Android Beginners Tutorial.