Problems receiving money as input in edittext

Asked

Viewed 604 times

-2

I’m building an app in Android Studio that gets a value inserted in a edittext, passes him on to BigDecimal and performs some operations.

In the field to enter this value, I marked android:inputType="numberDecimal", however, the keyboard is not inserting "." or "," in any way, and this prevents me from receiving the pennies of that amount.

What I really needed was a field with mask that by default displayed R$ 0.00 and the person only entered the numbers, but if I can simply receive the separator between whole value and cents would already serve me.

2 answers

1

Hello,

I found a solution in github.

<faranjit.currency.edittext.CurrencyEditText
        android:id="@+id/edt_currency"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:textColor="@android:color/black"
        app:groupDivider="."
        app:monetaryDivider=","
        app:locale="en_US"
        app:showSymbol="true" />

And to get the values:

double d = currencyEditText.getCurrencyDouble();
String s = currencyEditText.getCurrencyText();
  • Hello eliangela, I tried your solution, but it did not help, when I click on the point, it does not appear in edittext, and I also do not receive it, so I can not separate commas. Obg all the same

0

Set Edittext in XML:

<EditText
    android:id="@+id/edt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="double" />

And in Java use a Numberformat

public class Minha Activity extends Activity {

    private NumberFormat f;

    @Override
    public void onCreate(Bundle onSavedInstanceState) {

        f = NumberFormat.getNumberInstance();
        f.setMaximmumFractionDigits(2);
        f.setMinimumFractionDigits(2);

    }

}

And just go to Edittext and do:

f.format(valorAqui);

Which in your case will be more or less like this:

f.format(String.valueOf(edt.getText().toString()));

Don’t forget to import Numberformat. (java.text.Numberformat).

  • Oi António, the problem is that there is no such inputType, only number, numberDecimal and some others that do not serve. The problem is that when I click on the dot on the keyboard, it is not added to the value, and there is no way to have a penny value

Browser other questions tagged

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