EditText with Currency format

I have a EditText in which I want to display currency:

    input.addTextChangedListener(new CurrencyTextWatcher());


  • load Bitmap with RGB_565 via Picasso
  • Unable to compile under Android Studio 3.0 Canary 1
  • Replace common path parameters in retrofit 2 with okhttp
  • Android: What's the difference between a title bar and an aciton bar
  • How to trigger broadcast receiver when gps is turn on/off?
  • Calling an app from another app
  • public class CurrencyTextWatcher implements TextWatcher {
    boolean mEditing;
    public CurrencyTextWatcher() {
        mEditing = false;
    public synchronized void afterTextChanged(Editable s) {
        if(!mEditing) {
            mEditing = true;
            String digits = s.toString().replaceAll("\\D", "");
            NumberFormat nf = NumberFormat.getCurrencyInstance();
                String formatted = nf.format(Double.parseDouble(digits)/100);
                s.replace(0, s.length(), formatted);
            } catch (NumberFormatException nfe) {
            mEditing = false;

    I want to user to see a number-only keyboard, that is why I call


    on my EditText. However, it does not work. I see the numbers as typed in without any formatting. BUT: If I DO NOT set the inputType via input.setInputType(InputType.TYPE_CLASS_NUMBER), the formatting works perfectly. But the user must use the regular keyboard, which is not nice. How can I use the number keyboard and also see the correct currency formatting in my EditText? Thanks.

    Related posts:

    NavigationView get/find header layout
    Special characters in Android sms
    Button.setBackground(Drawable background) throws NoSuchMethodError
    Fix the Animation of a Circular ViewPager
    error: Only the original thread that created a view hierarchy can touch its views
    How to make multiple request and wait until data is come from all the requests in retrofit 2.0 - and...
  • How to turn screen off or send device to sleep
  • Android audio FFT to retrieve specific frequency magnitude using audiorecord
  • What is the best way to handle updates when the calendar syncs new events?
  • How to hide an item in a listview in Android
  • How to get malformed JSON in Retrofit 2
  • ProGuard Cannot Find Referenced Libraries
  • 2 Solutions collect form web for “EditText with Currency format”

    It is better to use InputFilter interface. Much easier to handle any kind of inputs by using regex. My solution for currency input format:

    public class CurrencyFormatInputFilter implements InputFilter {
    Pattern mPattern = Pattern.compile("(0|[1-9]+[0-9]*)?(\\.[0-9]{0,2})?");
    public CharSequence filter(
            CharSequence source,
            int start,
            int end,
            Spanned dest,
            int dstart,
            int dend) {
        String result = 
                dest.subSequence(0, dstart)
                + source.toString() 
                + dest.subSequence(dend, dest.length());
        Matcher matcher = mPattern.matcher(result);
        if (!matcher.matches()) return dest.subSequence(dstart, dend);
        return null;

    Valid: 0.00, 0.0, 10.00, 111.1
    Invalid: 0, 0.000, 111, 10, 010.00, 01.0

    How to use:

    editText.setFilters(new InputFilter[] {new CurrencyFormatInputFilter()});

    Try add this property in you xml declaration for you edit text:

    android:inputType="numberDecimal" or number or signed number

    See more info about android:inputType here.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.