Through SO I put together the following EditText addTextChangedListener event handler, it works fine except for a little improvement that I need. When I type, it populates the digits from right to left. I would rather it populates from left to right. Right now if I enter 50 it formats as $0.50, when I enter 50 I want it to format as $50.00
Below is the xml declaration and the Java code, please can someone point me to what I need to change to get the desired result.
XML declaration
<EditText
android:id="@+id/valueEditText"
android:layout_row="1"
android:hint="@string/hint_value_to_add"
android:imeOptions="actionNext"
android:inputType="numberDecimal"
style="@style/EnrollEditViewStyle" />
<requestFocus />
Java Code
inputValue = (EditText) findViewById(R.id.valueEditText);
inputValue.addTextChangedListener(new TextWatcher() {
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(current)) {
inputValue.removeTextChangedListener(this);
String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
String cleanString = s.toString().replaceAll(replaceable, "");
double parsed;
try {
parsed = Double.parseDouble(cleanString);
} catch (NumberFormatException e) {
parsed = 0.00;
}
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
inputValue.setText(formatted);
inputValue.setSelection(formatted.length());
inputValue.addTextChangedListener(this);
}
}
});