0

How do you check if an EditText is empty? I want to make it so that if the EditText is blank, a TextView value will be " " (space). If it's not, resume as normal. How do I go about doing this? Thanks for the help!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kyle Horkley
  • 911
  • 1
  • 9
  • 23

5 Answers5

23

to check Edittext as empty ,where myeditText is your edittext

if(myeditText.getText().toString().trim().length() == 0)

Or use below function

   private boolean isEmpty(EditText myeditText) {
            return myeditText.getText().toString().trim().length() == 0;
        }

If function return false means edittext is not empty and return true means edittext is empty

For more info see this best link Check if EditText is empty.

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
1

EditText has a method getText() which returns an instance of Editable and the Editable class has a method: length(). If the lenght is not 0, your EditText is not empty.

Bartosz Mikulski
  • 525
  • 3
  • 17
1
String text=yourEditText.getText().toString().trim();
if(text.equal("") || text==null) {
    //Do Somthing...
} 
else {
    //Do something else
}

Good luck

Ahmad
  • 69,608
  • 17
  • 111
  • 137
khurram
  • 1,362
  • 13
  • 24
0

If you go through EditText API reference, getText() will return the text as Editable. You can use it like

if(!TextUtils.isEmpty(myeditText.getText().toString().trim()) {
// Non-empty 
}
Kanak Sony
  • 1,570
  • 1
  • 21
  • 37
0

you can use this method from android framework

public static boolean isEmpty (CharSequence str)
Added in API level 1

Returns true if the string is null or 0-length.
Parameters
str     the string to be examined
Returns

    true if str is null or zero length 

http://developer.android.com/reference/android/text/TextUtils.html#isEmpty%28java.lang.CharSequence%29

ingyesid
  • 2,864
  • 2
  • 23
  • 21