EditText Enter the amount… here is a solution to the problem.
EditText Enter the amount
I’m working on a point of sale app.
So I want to let the user enter the purchase amount
Let’s say the user enters 100000, but I want it to automatically display
100,000
.1000000
becomes1,000,000
The second problem is that I don’t want users to be able to enter on their own
.
The third problem is that because this is money, we can’t let users enter 0 in the first place.
Any ideas?
So far, I’ve only been able to come up with inputType=numberDecimal
which isn’t very helpful.
Thank you very much
P.S.: I don’t need any decimal places
Solution
If you want
to use it in currency, add addTextChangedListener
to the edittext you want and then watch for the change and reformat it, here’s the 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);
}
}