1

Trying to get an integer from Edit Text entered by the user(such as Total Amount) and to display it by multiplying with value(such as 5%) and display it using TextView automatically.

For the 1st part i.e. simply displaying the integer from EditText to TextView got NumberFormatException Error.

''' temp = (Integer.parseInt(editText.getText().toString()));

textView.setText(temp);

'''

And for the second part i.e. automatically displaying the value from EditText to TextView, no idea how to approach.

kl23
  • 65
  • 3
  • 13

2 Answers2

1

Just use textwatcher and put the logic in it. Just make sure change the id of edittext and textview

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        String value = s.toString();
        double outputValue = Integer.parseInt(value) * 0.05;
        textView.setText(String.valueOf(outputValue));
    }
});

Make sure in edittext that it only capture the numerical value.

Ashish
  • 6,791
  • 3
  • 26
  • 48
1

you could try this approach

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void afterTextChanged(Editable s) {
            String userInputText = s.toString();
        try {
            val userInputAsNumbger = userInputText.toInt();
        }catch (e:NumberFormatException){
            print(e.message)
        }
        }
    });
     

to ensure it doesn't error

nt95
  • 455
  • 1
  • 6
  • 21