0

I am using filter to set the length of editText.
I set EditText length as 10 as per following.

TextView editVew = new TextView(R.id.txtAmt);
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(10);
editVew.setFilters(FilterArray);
editVew.setInputType(InputType.TYPE_CLASS_NUMBER);

My confusion : how to set fraction of 2 digits in my number and total length of my editview should not exceed 10 digits ?
If any body knows please reply.
Thanks

askimp
  • 155
  • 3
  • 10

3 Answers3

1

Try this to make your edittext support two digits after decimals.

EditText text = (EditText) findViewById(R.id.txtAmt);
text.addTextChangedListener(new TextWatcher() 
{
    public void afterTextChanged(Editable edittext) 
    {
        String str= edittext.toString();
        int posDot = str.indexOf(".");
        if (posDot <= 0) return;
        if (str.length() - posDot - 1 > 2)
        {
            edt.delete(posDot + 3, posDot + 4);
        }
    }

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});

And use android:maxLength="10" in xml to restrict your editText support 10 maximmum input digits

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60
0

Try this..

            maxLength = 10;
            smsType = "free";
            FilterArraySeventy[0] = new InputFilter.LengthFilter(maxLength);
            message.setFilters(FilterArrayten);
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Nitesh Khosla
  • 875
  • 8
  • 20
0

Solution for your Answer

amountEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
    amountEditText.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {}
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))
            {
                String userInput= ""+s.toString().replaceAll("[^\\d]", "");
                StringBuilder cashAmountBuilder = new StringBuilder(userInput);

                while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                    cashAmountBuilder.deleteCharAt(0);
                }
                while (cashAmountBuilder.length() < 3) {
                    cashAmountBuilder.insert(0, '0');
                }
                cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
                cashAmountBuilder.insert(0, '$');

                amountEditText.setText(cashAmountBuilder.toString());
                // keeps the cursor always to the right
                Selection.setSelection(amountEditText.getText(), cashAmountBuilder.toString().length());

            }

        }
    });

Same Question discussed here

Ref Link

Community
  • 1
  • 1
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95