Orestis Goulas
Orestis Goulas

Reputation: 47

Check if editText of decimal number type is empty

I am confused for a long time about an issue. In an if statement I want to check that an editText with decimal input IS NOT empty. I found many answers to check if it IS empty but I want the opposite.

Here is my code:

if(r>20 && r<25){
    startActivity(new Intent(Knees.this, HandJoints.class));
} else {
    Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\nTry again", Toast.LENGTH_SHORT);
    fail.show();
    elbow_r.setText("");
}               
break;

So in the first input statement I want to add an AND so as to prevent proceeding to the next activity in case that r field is empty. Besides this my application is running except the case I have no entry to that field.

Thank you in advance for reviewing my problem.

Upvotes: 2

Views: 972

Answers (3)

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Try something like this:

if(!TextUtils.isEmpty(elbow_r.getText().toString())){
    double r = Double.parseDouble(elbow_r.getText().toString());
    if(r>20 && r<25){
        startActivity(new Intent(Knees.this, HandJoints.class)
    } else {
   Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\n Try again", Toast.LENGTH_SHORT);
   fail.show();
   elbow_r.setText("");
   }
}

and don't forget to add android:inputType="number|numberDecimal" in your edittext xml

Upvotes: 0

Orestis Goulas
Orestis Goulas

Reputation: 47

This is my whole override method

 next1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            double r = Double.parseDouble(elbow_r.getText().toString());
            //startActivity(new Intent(Knees.this, HandJoints.class));
            switch (v.getId()) {
            case R.id.button2HandJoints:
                if(r>20 && r<25 && !TextUtils.isEmpty(elbow_r.getText())){
                    startActivity(new Intent(Knees.this, HandJoints.class));
                }else {
                    Toast fail=Toast.makeText(Knees.this, "Erased temperature/s not accepted\n                     Try again", Toast.LENGTH_SHORT);
                    fail.show();
                    elbow_r.setText("");}


                break;
            default:
                break;


        }
        }
    });

Upvotes: 0

Vucko
Vucko

Reputation: 7479

You can use this to check if a string is not empty:

if(!TextUtils.isEmpty(eblow_r.getText())){
    //do something
}

I'm guessing elbow_r is the name of your EditText.

Simply adding a ! in front of anything will make it not that, that's some Java basics.

Upvotes: 1

Related Questions