Raaj Lokanathan
Raaj Lokanathan

Reputation: 479

Check textfield is empty in Android Studio

I am trying to check whether my textfield is empty for validation purpose but i am getting an error message cannot resolve method isEmpty

This is my partial coding:

private void addMovie(){
        DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
        if(getIntent().getExtras()== null){
            databaseHandler.insertRow(
                    mvidEditText.getText().toString(), 
                    mvtitleEditText.getText().toString(),
                    mvtypeEditText.getText().toString(), 
                    mvstoryEditText.getText().toString(), 
                    mvratingEditText.getText().toString(), 
                    mvlanguageEditText.getText().toString(),
                    Integer.parseInt(mvruntimeEditText.getText().toString()));
            if (mvidEditText.isEmpty() || mvtitleEditText.matc) {
                Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
                return;
            }
        }else {
            databaseHandler.updateRow(rowID,
                    mvidEditText.getText().toString(), 
                    mvtitleEditText.getText().toString(),
                    mvtypeEditText.getText().toString(), 
                    mvstoryEditText.getText().toString(), 
                    mvratingEditText.getText().toString(), 
                    mvlanguageEditText.getText().toString(),
                    Integer.parseInt(mvruntimeEditText.getText().toString()));

        }
    }

Are there any ways to do this? I did some research from stack overflow too.Thank you.

Upvotes: 1

Views: 12855

Answers (6)

Syed Ali Shahzil
Syed Ali Shahzil

Reputation: 1224

Now in new version getText() is not working directly so use only text like this

 if (enter_name.text.toString().isEmpty()) {


  }

Upvotes: 2

Umesh Verma
Umesh Verma

Reputation: 876

to check Edittext is empty

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

Or use below function

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

Upvotes: 0

kishan
kishan

Reputation: 54

if(mvidEditText.getText().toString().equals("")){print message here}

Upvotes: 0

user4571931
user4571931

Reputation:

you can try following for checking empty value for edittext

mvidEditText.getText().toString().isEmpty();

where isEmpty returns true if length of this string is 0.

Upvotes: 0

LK Yeung
LK Yeung

Reputation: 3496

if(mvidEditText.getText().length() == 0){}

Upvotes: 0

Amit K. Saha
Amit K. Saha

Reputation: 5951

As far as my knowledge goes, there is no method isEmpty() in EditText class. you should do like this-

    if(!TextUtils.isEmpty(editTextRef.getText().toString())){
        ///.... your remaining code if the edittext is not empty
    }

Upvotes: 1

Related Questions