0

I want to create an Eddittext to type in currency values with 2 decimals from left to right. If there´s no value it shows 0.00, and as the user types the text should change acording to these rules: Input rules

I´ve tried getting it done using TextWatcher like in a similar question but I couldnt get it done as it kept calling TextWatcher after updating the text.

IBueno
  • 83
  • 10

2 Answers2

1

I finally got it working just as I wanted using a TextWatcher with this code, hope it helps someone:

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if ((count - before) > 0) {
                String text = s.toString().replace(',', '.');
                text = text.replace("..", ".");
                if (text.equals(".")) {
                    text = "0,00";
                    amount_field.setText(text);
                    amount_field.setSelection(2);

                } else {
                    int counter = 0;
                    for (int i = 0; i < text.length(); i++) {
                        if (text.charAt(i) == '.') {
                            counter++;
                            if (counter > 1) {
                                break;
                            }
                        }
                    }

                    if (counter > 1) {
                        StringBuilder sb = new StringBuilder(text);
                        sb.deleteCharAt(start);
                        amount_field.setText(sb.toString().replace('.', ','));
                        amount_field.setSelection(start);

                    } else {
                        Float value = Float.valueOf(text);
                        String result = String.format("%.2f", value);
                        amount_field.setText(result.replace('.', ','));
                        if (start != result.length()) {
                            amount_field.setSelection(start + 1);
                        } else {
                            amount_field.setSelection(start);
                        }
                    }
                }
            }
        }
IBueno
  • 83
  • 10
0

Try this:

String yourStringToPutIntoTextView = String.format("%.2f", YourFloat);

Here an example:

List<Float> listTestValue = new ArrayList<Float>();
listTestValue.add(new Float(10));
listTestValue.add(new Float(10.10));
listTestValue.add(new Float(1010));
listTestValue.add(new Float(0));
listTestValue.add(new Float(0.9));
listTestValue.add(new Float(.12));
listTestValue.add(new Float(0.01));
for(Float f : listTestValue)
{
    String s = String.format("%.2f", f);
    System.out.println(s);
}

If you have noInput format string with f = 0, like this:

String noInput = String.format("%.2f", (float)0);

Note that the values ​​must be Float!

Output:

10,00

10,10

1010,00

0,00

0,90

0,12

0,01

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • Thanks for the answer but I was asking to modify values as you type them in an Edittext, not a function with an input and an output. – IBueno Oct 07 '15 at 06:11
  • Yes but you can change value on EditText value changed, with this method, if you don't have any ideas about this, I can update code for you – Michele Lacorte Oct 07 '15 at 06:14
  • 1
    It works fine as a function with input and output, but dealing with a Textwatcher in Edittext there are problems with placing cursor after changing the text, infinite loops and so. – IBueno Oct 07 '15 at 08:26