TextWatcher
Basically, a TextWatcher is used to keep watch on EditText while entering data into it. We can perform operation and keep watch on which characters are being entered or how many characters are being entered in the EditText.For Ex. We can use TextWatcher to have checks on the password field that minimum number of characters are entered or not, or We can check the password Strength depending on number of characters entered in Password field .
TextWatcher class has 3 methods and we need override these methods to implement our own functionality.
1: public abstract void afterTextChanged (Editable s)
This method is called to notify you that, somewhere within
s
, the text has been changed.
It is legitimate to make further changes to s
from
this callback, but be careful not to get yourself into an infinite
loop, because any changes you make will cause this method to be
called again recursively.
(You are not told where the change took place because other
afterTextChanged() methods may already have made other changes
and invalidated the offsets. But if you need to know here,
you can use setSpan(Object, int, int, int)
in onTextChanged(CharSequence, int, int, int)
to mark your place and then look up from here where the span
ended up.
2: public abstract void beforeTextChanged (CharSequence s, int start, int count, int after)
This method is called to notify you that, within
s
,
the count
characters beginning at start
are about to be replaced by new text with length after
.
It is an error to attempt to make changes to s
from
this callback.
3: public abstract void onTextChanged (CharSequence s, int start, int before, int count)
This method is called to notify you that, within
In this post I have given an Example in which we have a password field of type EditText , in which user will enter his password and we will show strength of password according to the number of characters entered by the user, we also show that maximum characters has been is entered when user enter 20 characters.
We have used following parameters to check Password Strength
Password Length Password Strengthness
1-5 Easy
6-9 Medium
10-14 Strong
15-20 Strongest
We create an Object of TextWatcher and override required methods
Declare the Objects
EditText editTextPassword;
TextView textViewPasswordStrengthIndiactor;
inside onCreate()
editTextPassword.addTextChangedListener(mTextEditorWatcher);
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#E3FFC8"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:layout_marginTop="90dp"
android:textStyle="bold"
android:textColor="#520029"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Watcher Demo"
android:textSize="30dp" />
<!-- EditText to get the Password -->
<EditText
android:layout_marginTop="40dp"
android:textSize="23dp"
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter Your Password"
android:gravity="center_horizontal"
android:maxLength="20">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#B8FFB8">
<TextView
android:id="@+id/textViewPassword"
android:textColor="#000099"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Password Strength : "
android:layout_gravity="center_vertical"/>
<!-- TextView to show the Password Strength -->
<TextView
android:id="@+id/textViewPasswordStrength"
android:textColor="#005C00"
android:textStyle="bold"
android:textSize="23dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Eneterd"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity
{
// Create Object of EditText and TextWatcher
EditText editTextPassword;
TextView textViewPasswordStrengthIndiactor;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the Reference of EditText
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
textViewPasswordStrengthIndiactor=(TextView)findViewById(R.id.textViewPasswordStrength);
// Attach TextWatcher to EditText
editTextPassword.addTextChangedListener(mTextEditorWatcher);
}
// EditTextWacther Implementation
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// When No Password Entered
textViewPasswordStrengthIndiactor.setText("Not Entered");
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
if(s.length()==0)
textViewPasswordStrengthIndiactor.setText("Not Entered");
else if(s.length()<6)
textViewPasswordStrengthIndiactor.setText("EASY");
else if(s.length()<10)
textViewPasswordStrengthIndiactor.setText("MEDIUM");
else if(s.length()<15)
textViewPasswordStrengthIndiactor.setText("STRONG");
else
textViewPasswordStrengthIndiactor.setText("STRONGEST");
if(s.length()==20)
textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");
}
};
}
s
,
the count
characters beginning at start
have just replaced old text that had length before
.
It is an error to attempt to make changes to s
from
this callback.TextWatcher Example
In this post I have given an Example in which we have a password field of type EditText , in which user will enter his password and we will show strength of password according to the number of characters entered by the user, we also show that maximum characters has been is entered when user enter 20 characters.
We have used following parameters to check Password Strength
Password Length Password Strengthness
1-5 Easy
6-9 Medium
10-14 Strong
15-20 Strongest
How to Attach TextWatcher to EditText
We create an Object of TextWatcher and override required methods
Declare the Objects
EditText editTextPassword;
TextView textViewPasswordStrengthIndiactor;
inside onCreate()
editTextPassword.addTextChangedListener(mTextEditorWatcher);
Full Code :
main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#E3FFC8"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/textView1"
android:layout_marginTop="90dp"
android:textStyle="bold"
android:textColor="#520029"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text Watcher Demo"
android:textSize="30dp" />
<!-- EditText to get the Password -->
<EditText
android:layout_marginTop="40dp"
android:textSize="23dp"
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter Your Password"
android:gravity="center_horizontal"
android:maxLength="20">
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#B8FFB8">
<TextView
android:id="@+id/textViewPassword"
android:textColor="#000099"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Password Strength : "
android:layout_gravity="center_vertical"/>
<!-- TextView to show the Password Strength -->
<TextView
android:id="@+id/textViewPasswordStrength"
android:textColor="#005C00"
android:textStyle="bold"
android:textSize="23dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Eneterd"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
{
// Create Object of EditText and TextWatcher
EditText editTextPassword;
TextView textViewPasswordStrengthIndiactor;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the Reference of EditText
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
textViewPasswordStrengthIndiactor=(TextView)findViewById(R.id.textViewPasswordStrength);
// Attach TextWatcher to EditText
editTextPassword.addTextChangedListener(mTextEditorWatcher);
}
// EditTextWacther Implementation
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// When No Password Entered
textViewPasswordStrengthIndiactor.setText("Not Entered");
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void afterTextChanged(Editable s)
{
if(s.length()==0)
textViewPasswordStrengthIndiactor.setText("Not Entered");
else if(s.length()<6)
textViewPasswordStrengthIndiactor.setText("EASY");
else if(s.length()<10)
textViewPasswordStrengthIndiactor.setText("MEDIUM");
else if(s.length()<15)
textViewPasswordStrengthIndiactor.setText("STRONG");
else
textViewPasswordStrengthIndiactor.setText("STRONGEST");
if(s.length()==20)
textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");
}
};
}
New Advance Topics: Android LiveWallpaer Tutorial
Android ImageSwitcher Android TextSwitcher Android ViewFlipper
Android Gesture Detector Handling/Detecting Swap Events Gradient Drawable
Detecting Missed Calls Hide Title Bar GridView Animation
Android AlarmManager Android BootReceiver Vibrate Phone In a Desirable Pattern
Developing for Different Screen Sizes Showing Toast for Longer Time Publishing your App
How to publish Android App on Google Play
Beginning With Android
Android : Introduction(What is Android) Configuring Eclipse for Android Development
Creating Your First Android Project Understanding Android Manifest File of your android app
Advance Android Topics Customizing Android Views
Working With Layouts Working With Views
Understanding Layouts in Android Using Buttons and EditText in Android
Working with Linear Layout (With Example) Using CheckBoxes in Android
Nested Linear Layout (With Example) Using AutoCompleteTextView in Android Grid View
Relative Layout In Android ListView
Table Layout Android ProgressBar
Frame Layout(With Example) Customizing ProgressBar
Absolute Layout Customizing Radio Buttons
Grid Layout Customizing Checkboxes In Android
Android Components Dialogs In Android
Activity In Android Working With Alert Dialog
Activity Life Cycle Adding Radio Buttons In Dialog
Starting Activity For Result Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android Creating Customized Dialogs in Android
Returning Result from Activity Creating Dialog To Collect User Input
Android : Service DatePicker and TimePickerDialog
BroadcastReceiver Using TimePickerDialog and DatePickerDialog In android
Menus In Android ListView:
Creating Option Menu Populating ListView With DataBase
Creating Context Menu In Android Populating ListView with ArrayList
ListView with Custom Adapter
Toast Working With SMS
Customizing Toast In Android How to Send SMS in Android
Customizing the Display Time of Toast How To Receive SMS
Customizing Toast At Runtime Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time
TelephonyManager Storage: Storing Data In Android
Using Telephony Manager In Android SharedPreferences In Android
Reading and Writing files to Internal Stoarage
Working With Incoming Calls DataBase : Introduction of SQLiteDataBase
How To Handle Incoming Calls in Android Working With Database in Android
How to Forward an Incoming Call In Android Creating Table In Android
CALL States In Android Inserting, Deleting and Updating Records In Table in Android
Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
Thanks friend!
ReplyDeleteThank you. But instead of changing textview can we display a toast message?
ReplyDeleteThank you but instead of changing textview can we display a toast message?
ReplyDeleteThanks.
ReplyDeleteThanks, great job
ReplyDeleteThanks, great job
ReplyDeletei want to do edittext.setText("hai") and i need to get this 'hai' from textwatcher help me thanks in advance
ReplyDeleteWhat's up mates, pleasant paragraph and fastidious urging commented at this place, I am in fact enjoying by these.
ReplyDelete