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.
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);
}
}
});
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