how to put monetary mask for Edittext in an Android app?

Asked

Viewed 5,485 times

0

how to put monetary mask for Edittext in an Android application only with numerical values without the R$, ie print in Edittext 2.99? If you have an example.

inserir a descrição da imagem aqui

2 answers

2

Modify the onChanged of Textedit via code that works. And you don’t even need a mask for it. Follow an example:

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

            }

            private String current = "";
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!s.toString().equals(current)) {
                    Locale myLocale = new Locale("pt", "BR");
                    //Nesse bloco ele monta a maskara para money
                    txtUnitario.removeTextChangedListener(this);
                    String cleanString = s.toString().replaceAll("[R$,.]", "");
                    Decimal parsed = Double.parseDouble(cleanString);
                    String formatted = NumberFormat.getCurrencyInstance(myLocale).format((parsed / 100));
                    current = formatted;
                    txtUnitario.setText(formatted);
                    txtUnitario.setSelection(formatted.length());

                    //Nesse bloco ele faz a conta do total (Caso a qtde esteja preenchida)
                    String qtde = txtQtdeLitros.getText().toString();

                    txtUnitario.addTextChangedListener(this);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

This is just a basic way to make a monetary field, there are many other ways to do and in my opinion, this type of field does not need a mask because you just say that it is currency type and treat the data using Decimal ( Always avoid working with Double or Float values when monetary ).
Detail, Using this logic instead of a mask vc will also give a super cool effect to Edittext as it will fill in from the pennies.

  • It’s just that I need to perform calculations, I can still use your code?

  • You can, no problem. In my system I make a calculation of liters x value, I use this same code for unitario value, liters and total and works 100%

  • Hello, I had a problem how can I save the generated value in the MYSQL database? I will save as String it saved in this format R$ 2.99 or I can save as Decimal? but it shows the following error after saving in String. I’ll post it in the edit.

1

You can use this lib: https://github.com/santalu/mask-edittext

Then just add edittext already with xml mask:

 <com.santalu.maskedittext.MaskEditText
android:id="@+id/et_phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_phone_number"
android:inputType="number"
app:mask="#,##"/>
  • I did the test worked but it only treats values in the format 1,20 and if case it needs to value 12,40 or 1200,00?

  • just change the mask the way you want for example: For 12.40 is app:Mask="##,##"

  • I need a Mask that allows only uppercase letters and numbers, no point, space or other character, Do you know if it fits? Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.