omerdoron
omerdoron

Reputation: 25

How to check if EditText (input type:number) is null?

I have a problem, I have an EditText element (input type is numbers only), and I am trying to check if the value is null when pressing a button.

My code:

    // Get the chosen number input
    EditText numberInput = (EditText) findViewById(R.id.numberPlainTextInput);
    int chosenNumber = Integer.parseInt(numberInput.getText().toString().trim());

    // Get the result text element to set it`s text
    TextView resultText = (TextView) findViewById(R.id.resultText);

    // If input number field is not null
    if (**@number is not null@**) { **@Do something here@** }

I know how to do it on PHP but I am new to Java. Thanks in advance!

Upvotes: 0

Views: 1709

Answers (2)

SpiritCrusher
SpiritCrusher

Reputation: 21043

You need to first check for blank or null and then parse it .

EditText numberInput = (EditText) findViewById(R.id.numberPlainTextInput);
    if(!TextUtils.isEmpty(numberInput.getText().toString().trim())) {
        int chosenNumber = Integer.parseInt(numberInput.getText().toString().trim());
        // Do your stuff here
    }

Upvotes: 2

92AlanC
92AlanC

Reputation: 1387

Use TextUtils.isEmpty(numberInput.getText());

Upvotes: 1

Related Questions