0

I am having problem in putting a check on EditText. I need the user to enter values between 1-100 and if the value exceeds the toast should display and the EditText input should get refreshed. Please do help me with the ontextChanged part. The application get's crashed after toast. Any help would be highly appreciated.

MainActivity.java

userInput is of type EditText

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            progress.setText(" "+i);

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    userInput.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            try {
                int j = Integer.valueOf(userInput.getText().toString());

                if(j > 100 || j < 1){
                    Toast.makeText(getApplicationContext(), "Please enter value between 1-100", Toast.LENGTH_SHORT).show();
                }
            } catch(NumberFormatException e) {
                //do whatever you like when value is incorrect
            }

        }

        @Override
        public void afterTextChanged(Editable editable) {

            String text = editable.toString();

            if (!TextUtils.isEmpty(text)) {

                seekBar.setProgress(Integer.parseInt(text));

            }

            else {

                seekBar.setProgress(0);
            }

        }
    });
Community
  • 1
  • 1
Waleed77
  • 3
  • 3

2 Answers2

0

Do this in onTextChanged method

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    try {
        int j = Integer.valueOf(userInput.getText().toString());

        if(j > 100 || j < 1){
             Toast.makeText(getApplicationContext(), "Please enter value between 1-100", Toast.LENGTH_SHORT).show();
             userInput.setText("")
        } else {
             seekBar.setProgress(j);
        }
    } catch(NumberFormatException e) {
        //do whatever you like when value is incorrect
    }
}

and remove implementation from afterTextChanged method

muminers
  • 1,190
  • 10
  • 19
  • Sir I haven't removed the afterTextChanged method it's working fine with it without adding else part. But please tell me what to add in the code to make make the wrong input clear from the EditText – Waleed77 Mar 20 '18 at 18:36
  • If you would like to remove the string from edit text than it's becoming tricky because you will change the text and onTextChanged method will be called again. But acctually you can use reference to this EditText inside of onTextChanged method. – muminers Mar 20 '18 at 18:39
0

Following @muminers answer, You should place your code inside onTextChangedMethod only because afterTextChanged method is called even when some control changes the value of editText without your interaction directly. It creates a kind of loop some sometimes. So when you type in something in edit text you did something in afterTextChanged method which indirectly change the same edit text then it enters a infinite loop which causes app to crash.