I am developing a Point Of Sales app.
So I would like to let user to enter the purchase amount
Let's say User input 100000 but I want it to automatically show up 100,000. and 1000000 become 1,000,000
The second problem is that, I don't want user to be able to input . themselves.
Third problem is that since this is money, we can't let user to enter 0 in the beginning.
Any ideas?
So far I can only come up with inputType=numberDecimal which is not really helpful.
Thank you very much
P.S.: I do not need any decimal places
if you want use in currency add addTextChangedListener to your desired edittext then monitor changes and reformat it , here is sample code
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
[your_edittext].removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
[your_edittext].setText(formatted);
[your_edittext].setSelection(formatted.length());
[your_edittext].addTextChangedListener(this);
}
}
For your first problem follow this link Thousand separator
For your second problem add this to your editext
android:digits="0123456789"
android:inputType="numberDecimal"
And for your third problem you have to use TextWatcher like this
editText1.addTextChangedListener(new TextWatcher(){
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (editText1.getText().toString().matches("^0") )
{
// Not allowed
Toast.makeText(context, "not allowed", Toast.LENGTH_LONG).show();
editText1.setText("");
}
}
@Override
public void afterTextChanged(Editable arg0) { }
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With