EditText Problems - Tried almost everything but it doesn't works

20 views Asked by At

Everything is fine but I just want my app to check if the EditText is Empty or not, If it is empty then display a message and if not then it should proceed with the following code. Any help would be appreciated

public void guessNumber(View view) {

    EditText guess = findViewById(R.id.guessText);

    int guessnumber = Integer.parseInt(guess.getText().toString());

    if(guessnumber>randomnumber) {
          makeToast("Lower");
    } else if(guessnumber<randomnumber) {
          makeToast("Higher");
    } else if(guessnumber==randomnumber) {
          makeToast("You go me ! Try Again");
          Random random = new Random();
          randomnumber = random.nextInt(20)+1;
    } else {
          makeToast("Enter Something First");
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random random = new Random();
    randomnumber = random.nextInt(20)+1;
}
1

There are 1 answers

0
Constantin On

Use TextUtils.isEmpty()(See doc):

if(TextUtils.isEmpty(guess.getText())){
   // display message
}else{
   guessNumber();
}