Android TextWatcher-Example?
In this tutorial show how to implement Textwatcher in android? explain below....
Textwatcher is usefull public method in android,Textwatcher is watch input text instantly then you can update data instantly. for example validate emailId, i have implement email Validation TextWatcher .
The TextWatcher used Some Public Method.
Public Method
This mehod is called notify somewhere text has been change.it is legitimate to make further changes.
beforTextChanged()
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
this method is called to within s the count character beginning at start are about to be replace by the new text with length after
onTextChanged()
@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) { }
this method is called to within s the count character beginning at start have just replace text had length before.
content_main.xml.
...................................................................................................................................................................
...................................................................................................................................................................
ActivityMain.java
...................................................................................................................................................................
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_gravity="center"
android:src="@drawable/my_heder_img">
</ImageView>
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="30dp"
android:id="@+id/editText2"
android:padding="10dp"
android:background="@drawable/register_cover"
android:layout_gravity="center_horizontal" /> </LinearLayout>
...................................................................................................................................................................
ActivityMain.java
...................................................................................................................................................................
import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; public class MainActivity extends Activity { String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; EditText edittextEmailId; @Override
public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.content_main); edittextEmailId=(EditText)findViewById(R.id.editText2); edittextEmailId.addTextChangedListener(new TextWatcher() { @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// instantly get input text
String email = edittextEmailId.getText().toString().trim();
//Check Email Pattern instantly
if (email.matches(EMAIL_PATTERN) && s.length() > 0)
{
edittextEmailId.setError(null); edittextEmailId.requestFocus();
}
else
{ edittextEmailId.setError("Invalid email address"); edittextEmailId.requestFocus();
}
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override
public void afterTextChanged(Editable s) {
}
});
}
}
...................................................................................................................................................................
0 comments