Question

I have edittext where I want to enter percentage value i.e decimals value, So I want to limit the user to enter only value less than 100 in the editText.

Example:1) if he want to enter value more than 100, it should not allow him to enter this.

2)If he wants t0 enter decimal value less than 100 i.e 50.5444 then it should allow him to enter this value

I have also found this link where I can set filter for integer value maximum to 100 but its not allowing me to enter decimal value

link: http://tech.chitgoks.com/2011/06/27/android-set-min-max-value-an-edittext-accepts/

Can anybody help me.

Was it helpful?

Solution

Hey you can use the TextWatcher, apply it to your EditText and implement the three methods it provides. Then, when you insert a new value, you can use a RegEx to check its validity.

Hope it helps!

OTHER TIPS

Here is the full implementation code I used

editTextSpLinePercent.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        String enteredValue = s.toString();
        if(checkNullValues(enteredValue)) {
            if(Float.parseFloat(enteredValue.trim()) >100.0f){
                AlertDialog.Builder builder = new AlertDialog.Builder(SpecialDiscountLineActivity.this);
                // builder.setCancelable(true);
                builder.setMessage("Percentage Value should be Less than 100");

                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                         editTextSpLinePercent.setText("");
                    }
                });           
                builder.show();            
            }
        }
    }
});


public static boolean checkNullValues(String valueToCheck) {
    //Log.i("Log","CheckForNullValues : "+valueToCheck);
    if(!(valueToCheck == null)) {
        String valueCheck = valueToCheck.trim();
        if(valueCheck.equals("") || valueCheck.equals("0")  ) {
            //  Log.i("Log","Returning false 0 or Blank");
            return false;
        }
        return true;
    }
    //Log.i("Log","Returning false null");
    return false;
}

You can actually modify the helpful code sniplet provided in the link mentioned in the question as follows. Also optionally you can include the number of max decimal places you want the percentage to have and use the pattern matcher to return "" if the input doesn't match the intended pattern.

private class PercentageInputFilter implements InputFilter {
    private float min;
    private float max;

    PercentageInputFilter(float min, float max) {
        this.min = min;
        this.max = max;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        try {
            // Get input
            String stringToMatch = dest.toString() + source.toString();
            float input = Float.parseFloat(stringToMatch);

            // Check if the input is in range.
            if (isInRange(min, max, input)) {
                // return null to accept the original replacement in case the format matches and text is in range.
                return null;
            }
        } catch (NumberFormatException nfe) {
        }
        return "";
    }

    private boolean isInRange(float min, float max, float input) {
        return input >= min && input <= max;
    }
}

You can use it like:

new PercentageInputFilter((float) 0.00, (float) 100.00);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top