Decimal separator comma (',') with numberDecimal inputType in EditText
A workaround (until Google fix this bug) is to use an EditText
with android:inputType="numberDecimal"
and android:digits="0123456789.,"
.
Then add a TextChangedListener to the EditText with the following afterTextChanged:
public void afterTextChanged(Editable s) {
double doubleValue = 0;
if (s != null) {
try {
doubleValue = Double.parseDouble(s.toString().replace(',', '.'));
} catch (NumberFormatException e) {
//Error
}
}
//Do something with doubleValue
}
inputType= numberDecimal doesn't accept comma, even when using android:digits= 0123456789.,
Use android:inputType="phone". :)
Use Comma as decimal separator for android EditText with MvvmCross
That's an Android bug that is unfixed since 2009.
You have to set android:digits
.
<EditText
local:MvxBind="Text PorcentagemDesconto"
android:inputType="number|numberDecimal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="%"
android:digits="0123456789,-"
android:focusableInTouchMode="true" />
If you want to deploy your app for different countries, you have to use a string resource android:digits="@string/localized_digits
with
values-de/strings.xml e.g. german
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="localized_digits">0123456789,-</string>
</resources>
values/strings.xml default
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="localized_digits">0123456789.-</string>
</resources>
Or use a more generic approach and set it via code:
// extension method
public static class EditTextExtension
{
public static void FixDigits(this EditText text)
{
var decimalSign = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
var digits = $"0123456789{decimalSign}-";
text.KeyListener = DigitsKeyListener.GetInstance(digits);
}
}
// usage
FindViewById<EditText>(Resource.Id.txtNumber).FixDigits();
Or you add thr functionality of the extension method FixDigits()
to your custom EditText class.
How to Automatically add thousand separators as number is input in EditText
You can use String.format()
in a TextWatcher
. The comma in the format specifier does the trick.
This does not work for floating point input. And be careful not to set an infinite loop with the TextWatcher.
public void afterTextChanged(Editable view) {
String s = null;
try {
// The comma in the format specifier does the trick
s = String.format("%,d", Long.parseLong(view.toString()));
} catch (NumberFormatException e) {
}
// Set s back to the view after temporarily removing the text change listener
}
Android EditText - allow only one decimal separator
Hopefully this will help someone. I needed to do this programatically.
Method 1:
editText.setKeyListener(DigitsKeyListener.getInstance(true,true));
Method 2: (method 1 didn't work for me)
First get the default decimal separator based on device locale:
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
defaultSeperator=Character.toString(symbols.getDecimalSeparator());
Then in your EditText TextWatcher's afterTextChanged add:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {
if(editable.toString().contains(defaultSeperator))
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
else
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789" + defaultSeperator));
}
}
Related Topics
Best Way to Work with Dates in Android SQLite
Calling Startactivity() from Outside of an Activity Context
How to Mock Location on Device
Android New Bottom Navigation Bar or Bottomnavigationview
Android Sdk Manager Won't Open
How to Provide Shadow to Button
What Exactly Does the Post Method Do
How to Change the Text Color of Menu Item in Android
Android Listview Not Refreshing After Notifydatasetchanged
How to Update UI in a Broadcastreceiver
Is There Any Way in Android to Get the Height of Virtual Keyboard of Device
Parse Dynamic Key JSON String Using Retrofit
Android Studio - Mergedebugresources Exception
Android Alarmmanager Not Working on Some Devices When the App Is Closed
Android: Customize Application's Menu (E.G Background Color)