Problems receiving money as input in edittext

-2

I'm building an app in Android Studio that gets a value entered in a edittext , passes it 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 that prevents me from receiving the cents of that value.

What I really needed was a field with a mask that would display $ 0.00 and the person would only enter the numbers, but if I can just get the tab between full value and pennies I would have been satisfied.

    
asked by anonymous 04.07.2018 / 12:30

2 answers

1

Hello,

I found a solution in the 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();
    
04.07.2018 / 15:56
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 look something like this:

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

Do not forget to import the NumberFormat. (java.text.NumberFormat) .

    
06.07.2018 / 16:31