Android: Check If Edittext Is Empty When Inputtype Is Set On Number/phone
I have an EditText in android for users to input their AGE. It is set an inputType=phone. I would like to know if there is a way to check if this EditText is null. I've already l
Solution 1:
You can check using the TextUtils class like
TextUtils.isEmpty(ed_text);
or you can check like this:
EditTexted= (EditText) findViewById(R.id.age);
Stringed_text= ed.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
//EditText is empty
}
else
{
//EditText is not empty
}
Solution 2:
First Method
Use TextUtil library
if(TextUtils.isEmpty(editText.getText().toString())
{
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
Second Method
privatebooleanisEmpty(EditText etText)
{
return etText.getText().toString().trim().length() == 0;
}
Solution 3:
Add Kotlin getter functions
val EditText.empty get() = text.isEmpty() // it == ""// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""
With these, you can just use if (edittext.empty) ...
or if (edittext.blank) ...
If you don't want to extend this functionality, the original Kotlin is:
edittext.text.isBlank()
// or
edittext.text.isEmpty()
Solution 4:
EditText textAge;
textAge = (EditText)findViewByID(R.id.age);
if (TextUtils.isEmpty(textAge))
{
Toast.makeText(this, "Age Edit text is Empty", Toast.LENGTH_SHORT).show();
//or type here the code you want
}
Solution 5:
I use this method for same works:
publicbooleancheckIsNull(EditText... editTexts){
for (EditTexteditText: editTexts){
if(editText.getText().length() == 0){
returntrue;
}
}
returnfalse;
}
Post a Comment for "Android: Check If Edittext Is Empty When Inputtype Is Set On Number/phone"