V K
V K

Reputation: 65

How do I replace comma with dot in EditText in case of Portuguese

The validation in EditText (numberDecimal) is working fine with English language. I mean, the decimal separator as expected.

I am trying to replace dot with comma in case of Portuguese.

I have tried some solutions but those solutions allow multiple commas where I am expecting the behaviour as dot.

Here is what I have tried but it's allowing multiple commas inside after text changed:

if(device lang is portoguese){

edittext.keyListener = DigitsKeyListener.getInstance("0123456789,")

}

Upvotes: 1

Views: 549

Answers (1)

Sunil Kumar
Sunil Kumar

Reputation: 1

Try with Locale. Get the locale instance of the user and based on locale like below:

  public static String formatNumber(double number, Locale locale) {
        NumberFormat formatter = DecimalFormat.getInstance(locale);
        formatter.setRoundingMode(RoundingMode.HALF_UP);
        formatter.setMaximumFractionDigits(MAXIMUM_FRACTION_DIGITS);
        formatter.setMinimumFractionDigits(MINIMUM_FRACTION_DIGITS);
        if (locale == null || formatter == null) {
            return "";
        }

        return format(number);
    }

Now, call formateNumber() method and set in your edit-text.

Upvotes: 0

Related Questions