Set error if the user tries to type after limit is reached in Android edittext
I tried via TextWatcher
's afterTextChanged
. All I want is display a toast message or set error on EditText
if the user tries to insert characters after the limit is reached. I think it is possible. But don't know how to achieve this.
Update
My EditText coding is
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLength="10"
android:layout_marginTop="150dp"
android:inputType="numberDecimal" >
Here if the user tries to enter more than 10 digit I want to display a toast or want to set error.
Update
editText1.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (editText1.getText().length() >= 10)
Toast.makeText(getApplicationContext(), "hi",
Toast.LENGTH_SHORT).show();
return false;
}
});
Solution 1:
On key listener doesn't work on Soft Input Keyboard.. From the Android reference at:
http://developer.android.com/reference/android/view/View.OnKeyListener.html
View.OnKeyListener
Class Overview Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.
So that means that TextWatcher is only your friend with editboxes and keyboard Inputs..