I have a Edit-text in which user can enter amount, I want few limitations on Edit-text
- Amount can not be greater than 100000
- User enter amount up to two decimal places like 99999.50
-EDITED- Solved
How do I achieve this, do I have to create a filter or I can achieve it using Regular Expression in myEdittext.addTextChangedListener(new TextWatcher() ...
myAmountEditText.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) {
if(amount!=null && amount.length()>=1 ){
if(amount.contains(".")){
int indexOFdec = amount.indexOf(".");
if(indexOFdec >=0) {
if(amount.substring(indexOFdec).length() >3){
amount = amount.substring(0, amount.length()-1);
myAmountEditText.setText(amount);
myAmountEditText.setSelection(myAmountEditText.getText().length());
}
}
}
Double a = Double.parseDouble(amount.toString());
if(a>0 && a<100000.00){
//my stuff
}else{
Toast.makeText(context, "amount greater than 100000", Toast.LENGTH_SHORT).show();
}
}
});
You can use this code:
I think we can work for that like this: