Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display error if user enters only spaces in EditText - Android

I want to show error if user only enters spaces in EditText but i don't know how?

Here is my simple work

if (tName.getText().toString().matches(" ")) {
    Toast.makeText(MainActivity.this, "Must Add Your Name Here", Toast.LENGTH_SHORT).show();
}
like image 818
Mahesh Babariya Avatar asked Dec 17 '15 06:12

Mahesh Babariya


1 Answers

Try with this code

 if(!editText.getText().toString().trim().isEmpty()){
           editText.setError("Empty spaces are not allowed");
           editText.setFocusable(true);
           Toast.makeText(MainActivity.this, "Must Add Your Name Here", Toast.LENGTH_SHORT).show();
    }
like image 59
Madhukar Hebbar Avatar answered Oct 01 '22 01:10

Madhukar Hebbar