periods not available in keypad to enter in edittext in android

Manikandan

I have a EditText, where I need to enter price. I need to show $ symbol before the digits and two decimal after period(.55) and period should be once. I used the following code. But when I hit "." it doesn't appear in the EditText.

<EditText
                    android:id="@+id/editTextForAddItemPrice"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="20dp"
                 android:inputType="numberDecimal"
                  android:digits="0123456789.,$"
                    android:layout_marginTop="20dp"
                    android:layout_toRightOf="@+id/textForAddItemPrice"
                    android:background="@color/app_bg_color"
                    android:gravity="right"
                    android:textColor="@color/txt_color_gray"
                    android:textSize="17dp" />

editTxtForPrice.addTextChangedListener(new TextWatcher() {
            boolean isEdiging;
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            //  String sText = editTxtForPrice.getText().toString();
                if(s.length()>0)
                {
                 if(isEdiging) return;
                    isEdiging = true;

                    String str = s.toString().replaceAll( "[^\\d]", "" );
                    if(!str.equals(""))
                    {
                    double s1 = Double.parseDouble(str);

                    NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
                    ((DecimalFormat)nf2).applyPattern("$ ###,###.###");
                    s.replace(0, s.length(), nf2.format(s1));


                    }
                    isEdiging = false;  
                    }
            }  
        });
Manikandan

I used a textwatcher, which solved my problem.

editTxtForPrice.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                if (!s.toString().equals(current)) {

                    editTxtForPrice.removeTextChangedListener(this);

                    String cleanString = s.toString().replaceAll("[$,.]", "");

                    double parsed = Double.parseDouble(cleanString);
                    String formatted = NumberFormat.getCurrencyInstance()
                            .format((parsed / 100));

                    current = formatted;
                    editTxtForPrice.setText(formatted);
                    editTxtForPrice.setSelection(formatted.length());

                    editTxtForPrice.addTextChangedListener(this);
                }

            }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically disable keypad popup in android studio for an EditText

From Dev

Disable Enter in editText android

From Dev

Android keypad view is in front of the EditText when touched to edit

From Dev

Android EditText - Not able to enter value

From Dev

Android EditText enter key listener

From Dev

Android EditText with inputType and Enter button

From Dev

Android EditText - Not able to enter value

From Dev

Stuck with handling Enter in android edittext

From Dev

Enter +/- float value in edittext in android

From Dev

Android EditText : How to avoid user enter smileys?

From Dev

How to enter special characters in an EditText in Android?

From Dev

Change focus of edittext on Enter pressed_android

From Dev

Android [ENTER]-key action of a multiline edittext in a dialog

From Dev

Change focus of edittext on Enter pressed_android

From Dev

android - enter multiple string values to edittext

From Dev

Is there a keypad widget for Android?

From Dev

ImGui use both 'enter' and numeric keypad 'enter' as ImGuiKey_Enter

From Dev

How to hide keypad when editText is in focus

From Dev

Enter key starting a new line in EditText instead of submitting the text (Android)

From Dev

setError does not clear after enter text in edittext in Android

From Dev

setError does not clear after enter text in edittext in Android

From Dev

How to let user 'Enter' an answer and make editText disappear? [ANDROID STUDIO]

From Dev

Sending the enter key located on the lower right of the numeric keypad in Dragon NaturallySpeaking

From Dev

SearchView doesn't capture "ENTER" from keyboard or keypad

From Dev

android:digits not allowing next button in keypad

From Dev

Android : TYPE_CLASS_PHONE keypad issue

From Dev

The screen gets jumbled when the keypad opens in android

From Dev

In a fragment implementation, the ListView below EditText disappears after setting keypad element (of EditText view) to hide

From Dev

Submit an AlertDialog.Builder containing an EditText by pressing enter on the keyboard in android studio

Related Related

  1. 1

    Dynamically disable keypad popup in android studio for an EditText

  2. 2

    Disable Enter in editText android

  3. 3

    Android keypad view is in front of the EditText when touched to edit

  4. 4

    Android EditText - Not able to enter value

  5. 5

    Android EditText enter key listener

  6. 6

    Android EditText with inputType and Enter button

  7. 7

    Android EditText - Not able to enter value

  8. 8

    Stuck with handling Enter in android edittext

  9. 9

    Enter +/- float value in edittext in android

  10. 10

    Android EditText : How to avoid user enter smileys?

  11. 11

    How to enter special characters in an EditText in Android?

  12. 12

    Change focus of edittext on Enter pressed_android

  13. 13

    Android [ENTER]-key action of a multiline edittext in a dialog

  14. 14

    Change focus of edittext on Enter pressed_android

  15. 15

    android - enter multiple string values to edittext

  16. 16

    Is there a keypad widget for Android?

  17. 17

    ImGui use both 'enter' and numeric keypad 'enter' as ImGuiKey_Enter

  18. 18

    How to hide keypad when editText is in focus

  19. 19

    Enter key starting a new line in EditText instead of submitting the text (Android)

  20. 20

    setError does not clear after enter text in edittext in Android

  21. 21

    setError does not clear after enter text in edittext in Android

  22. 22

    How to let user 'Enter' an answer and make editText disappear? [ANDROID STUDIO]

  23. 23

    Sending the enter key located on the lower right of the numeric keypad in Dragon NaturallySpeaking

  24. 24

    SearchView doesn't capture "ENTER" from keyboard or keypad

  25. 25

    android:digits not allowing next button in keypad

  26. 26

    Android : TYPE_CLASS_PHONE keypad issue

  27. 27

    The screen gets jumbled when the keypad opens in android

  28. 28

    In a fragment implementation, the ListView below EditText disappears after setting keypad element (of EditText view) to hide

  29. 29

    Submit an AlertDialog.Builder containing an EditText by pressing enter on the keyboard in android studio

HotTag

Archive