3

I am developing a Point Of Sales app.

So I would like to let user to enter the purchase amount

  1. Let's say User input 100000 but I want it to automatically show up 100,000. and 1000000 become 1,000,000

  2. The second problem is that, I don't want user to be able to input . themselves.

  3. 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

JayVDiyk
  • 4,277
  • 22
  • 70
  • 135
  • what if user want to input 10000, you will make it 100? – Vivek Mishra Feb 21 '16 at 14:02
  • 1
    for your second problem you can specify digits for edittext by using digits keyword in edittext xml – Vivek Mishra Feb 21 '16 at 14:04
  • 1
    for your third problem you can see this link http://stackoverflow.com/questions/17490054/android-the-first-digit-in-the-edit-text-can-not-be-zero – Vivek Mishra Feb 21 '16 at 14:05
  • 1
    and for your first problem you can see this link http://stackoverflow.com/questions/12338445/how-to-automatically-add-thousand-separators-as-number-is-input-in-edittext – Vivek Mishra Feb 21 '16 at 14:07

3 Answers3

3

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);
    }
}
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
  • I don't understand - why do you need last string '[your_edittext].addTextChangedListener(this);' for? – Kirguduck Apr 19 '22 at 11:13
  • you need to de-attach the listener to formate the string cause if you don't it will keep firing and after you finish re-attach the listener again – Mina Fawzy Apr 19 '22 at 15:18
1

You could implement one or more InputFilters to enforce those constraints on your EditText. It is possible to attach multiple filters on an EditText by using it's setFilters method.

You could also use TextWatchers to achieve the same thing. However, using InputFilter makes a little bit more sense as it allows you to alter the text without having to call the setText method after each change you make in the input.

Alexandru Pele
  • 1,123
  • 7
  • 12
1

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) {}
}); 
Community
  • 1
  • 1
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84