I want to show error if the user enters blank value in the edittext.But i am not getting the way how could i do this .This is how i want like this:
if(TextUtils.isEmpty(firstName.getText().toString()){
firstName.setError("TEXT ERROR HERE");
}
Or you can also use TextInputLayout which has some useful method and some user friendly animation
I know it's too late, but in case someone still need help. Here is the working solution.
Setting an error
is pretty straight forward. But it will be displayed to user, when he request Focus
on it. So to do the both thing on your own, User this code
.
firstName.setError("Enter FirstName");
firstName.requestFocus();
u can use this :
@Override
public void afterTextChanged(Editable s) {
super.afterTextChanged(s);
if (s.length() == Bank.PAN_MINIMUM_RECOGNIZABLE_LENGTH + 10) {
Bank bank = BankUtil.findByPan(s.toString());
if (null != bank && mNewPanEntered && !mNameDefined) {
mNewPanEntered = false;
suggestCardName(bank);
}
private void suggestCardName(Bank bank) {
mLastSuggestTime = System.currentTimeMillis();
if (!bank.getName().trim().matches(getActivity().getString(R.string.bank_eghtesadnovin))) {
inputCardNumber.setError(R.string.balance_not_enmb, true);
}
}
You can show error as PopUp of EditText
if (editText.getText().toString().trim().equalsIgnoreCase("")) {
editText.setError("This field can not be blank");
}
and that will be look a like as follows
firstName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (firstName.getText().toString().length <= 0) {
firstName.setError("Enter FirstName");
} else {
firstName.setError(null);
}
}
});
you could use an onchange event to trigger a validation and then you can display a toast if this is enough for you
private void showError() {
mEditText.setError("Password and username didn't match");
}
Which will result in errors shown like this:
And if you want to remove it:
textView.setError(null);