how to check if a string is empty or null in Android Studio

24,888

Just Use

if(TextUtils.isEmpty(/* your String*/)){
  // String is empty or null
}else {
  // string has value
}
Share:
24,888
John Champion
Author by

John Champion

Updated on July 09, 2022

Comments

  • John Champion
    John Champion almost 2 years

    In my project I have an edit text field and its name is editText also I have assign mTextEmri = (EditText) findViewById(R.id.editText); I want to get its value and I have done it with the following statment String emridhene = mTextEmri.getText().toString();

    Now I want to check it if it is null or empty I have tried this code but it is not working

    if (emridhene.toString() == null || emridhene.toString() == "") {
                    mTextViewKonfirm.setText("Ju lutem shkruani nje Emer");
                }
               else{
                    mTextViewKonfirm.setText(emridhene);
                }
    

    How can I make it to work. Thanks.