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!
Asked
Active
Viewed 4.5k times
0
OneCricketeer
- 179,855
- 19
- 132
- 245
Kyle Horkley
- 911
- 1
- 9
- 23
5 Answers
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.
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
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
ingyesid
- 2,864
- 2
- 23
- 21
-
if(myeditText.getText().toString().isEmpty()){ } user this pattern – tapos ghosh Feb 06 '17 at 11:07