-1

I would like to set mask form on Edittext with 2 digits and 2 decimal -> XX.XX In Hint properties can't to lock form on I need. Hint will suggestion user only. In Visual Basic I can use Mask properties on MaskTextBox. It can fixed form XX.XX then I not type decimal point , decimal point still permanently on it.

<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:id="@+id/editText13"
            android:gravity="center"
            android:maxLength="5"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textSize="24sp"
            android:layout_weight="1"
            android:labelFor="@+id/editText13"
            android:hint="XX.XX" />

1 Answers1

0

Try this

private String amtPattern = "^(([1-9]\\d{0,2}(,\\d{3})*)|(([1-9]\\d*)?\\d))(\\.\\d\\d)?$";

Pattern pattern = Pattern.compile(amtPattern);

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                String amt = s.toString();
                Matcher matcher = pattern.matcher(amt);
                if (matcher.matches()) {
                    editText.setTextColor(Color.BLACK);
                } else {
                    editText.setTextColor(Color.RED);
                    //alert user for wrong input
                }
            }
        });

You can also keep your edittext length to 5(if u want restrict user to enter more than 5 number. keep your edittext input type as numberDecimal

Nas
  • 2,158
  • 1
  • 21
  • 38