Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText to Input Money Amount

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

like image 415
JayVDiyk Avatar asked Nov 08 '25 04:11

JayVDiyk


2 Answers

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);
    }
}
like image 73
Mina Fawzy Avatar answered Nov 09 '25 20:11

Mina Fawzy


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) {}
}); 
like image 31
Vivek Mishra Avatar answered Nov 09 '25 21:11

Vivek Mishra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!