14

I have a TextWatcher that enables a button when all the EditTexts length() are != 0. I now want to add the ability to also make sure the number is positive. I tried putting a new if() inside the other if() to check if >0 but for some reason it doesn't work.

So what I need is to make sure all EditText are not empty and positive numbers have been entered then enable the number.

This is what I have,

    public TextWatcher localWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
    }

    // When the text is edited, I want to check if all textViews are filled
    @Override
    public void afterTextChanged(Editable s) {

        // Check if any of the TextViews are == 0, if so the button remains disabled
        if      (et1local.getText().toString().length() == 0
                || et2local.getText().toString().length() == 0
                || et3local.getText().toString().length() == 0
                || et4local.getText().toString().length() == 0
                || et5local.getText().toString().length() == 0
                || et6local.getText().toString().length() == 0
                || et7local.getText().toString().length() == 0
                || et8local.getText().toString().length() == 0
                || et9local.getText().toString().length() == 0) {

            if(et1local){




        localCalculateButton.setEnabled(false);
            }

        }

        // When all are filled enable the button
        else {
            localCalculateButton.setEnabled(true);
        }
    }
};

This works fine but how to also check if the number is positive, any help wpuld be great thanks.

Getek99
  • 479
  • 2
  • 5
  • 18

5 Answers5

57

You should use the attr of EditText:

android:digits="0123456789."
android:inputType="number"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits

So user will be able enter only digits without minus.

UPDATED: Something like that, to prevent first sign from 0:

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable edt) {
            if (edt.length() == 1 && edt.toString().equals("0"))
                editText.setText("");
        }

        // ...
    });
Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100
Vito
  • 1,414
  • 1
  • 13
  • 22
  • Hi Vito I have this implemented, but I don't want to be able to put a 0 in the first digit space as this would be negative. – Getek99 Oct 24 '14 at 15:08
  • 1
    @Getek99 If you set both `android:digits="0123456789." android:inputType="numberDecimal"` Then it should only accept formatted numbers too, (and remove leading 0s) – IAmGroot Oct 24 '14 at 15:10
  • 1
    did you tried Doomsknight's suggestion with android:inputType="numberDecimal"? – Vito Oct 24 '14 at 15:20
  • if android:inputType="numberDecimal" didn't help, than you must hanle it in TextWatcher. And if my answer helps you, please accept it =) – Vito Oct 24 '14 at 15:30
  • Yes, I tried android:digits="0123456789." android:inputType="numberDecimal" – Getek99 Oct 24 '14 at 16:06
  • It helps as it allows only number in but the 0 is still allowed in before the rest of the numbers. How to check for leading zero? – Getek99 Oct 24 '14 at 16:07
  • You need check length of editable text inside afterTextChanged method .if length is 1 and string is 0 than remove 0 . – Vito Oct 24 '14 at 16:48
  • Maybe my code have some compilation errors, as I have not Eclipse with me now=) – Vito Oct 24 '14 at 16:56
  • Thanks for you help. It was as simple as et1local.getText().toString().length() == 0 || Integer.parseInt(et1local.getText().toString()) <= 0 – Getek99 Oct 24 '14 at 23:36
  • Thanks! Very useful! – Kingsley Ijike Jan 11 '18 at 15:27
2

For each EditText, you have to do this :

if (Integer.parseInt(et1local.getText().toString()) > 0)

Do a function to ensure it's a number

private boolean isPositive(EditText et)
{
    try
    {
        return Integer.parseInt(et.getText().toString()) > 0;
    }
    catch (Exception e)
    {
        return false;
    }
}

Use it like that :

if (et1local.getText().toString().length() == 0 || !isPositive(et1local)
 || et2local.getText().toString().length() == 0 || !isPositive(et2local)
// [...]
ToYonos
  • 16,469
  • 2
  • 54
  • 70
0

This is a bit more long winded than ToYonos answer, but may help understanding the principles. It also simplifies your if block a bit.

private boolean isEditTextEmptyOrNegative(EditText editText) {
    // check if editText is empty
    if (editText.getText().toString().length() == 0)
        return true;

    // get text from editText
    String text = editText.getText().toString();

    try {
        // try to parse text to int
        int textInt = Integer.valueOf(text);

        // if int is > 0, return false (not empty or negative), else return true (int is negative)
        if (textInt > 0)
            return false;
        else
            return true;
    } catch (NumberFormatException nfe) {
        // catch NumberFormatException, text entered is not a valid int, treat it as empty (you may want to handle differently)
        return true;
    }
}

and your if block would be something like this:

if (isEditTextEmptyOrNegative(et1local) || isEditTextEmptyOrNegative(et2local) || ... )
invertigo
  • 6,336
  • 5
  • 39
  • 64
0

Just remove minus signs in onTextChanged.

public void onTextChanged(CharSequence s, int start, int before,
        int count) {

      et1Local.setText(et1local.getText().toString().replace"-",""));
      .....
      et9Local.setText(et9local.getText().toString().replace("-",""));
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
greenapps
  • 11,154
  • 2
  • 16
  • 19
0

For me, simply setting android:inputType="number" (without the android:digits part) did the trick just fine.
And I had no problems with leading zeros converting to negative numbers as you seemed to have (I'm getting my inputs with Integer.valueOf(binding.myEditText.getEditableText().toString())).

Rautermann
  • 334
  • 3
  • 10