Android - Limit only one decimal point enter into a EditText

10,251

Solution 1

Play around with this however you like it.

myEditText.setKeyListener(DigitsKeyListener.getInstance(true,true));

Returns a DigitsKeyListener that accepts the digits 0 through 9, plus the minus sign (only at the beginning) and/or decimal point (only one per field) if specified.

Solution 2

You can set it up in the xml layout:

<EditText
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:inputType="numberDecimal"    
/>

Solution 3

There's flags you can set so you don't have to do what rochdev (no offense of course) posted.

mEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

This will only allow numbers to be entered and only one decimal point.

Share:
10,251
KevinM
Author by

KevinM

Frontend and Backend Developer Specialize in Java, Objective-C, Ruby On Rails and C#. I've dabbled in Python. Platforms I develop for are Cocoa Touch, Cocoa, Android, Windows 8, and backend API's with Ruby On Rails.

Updated on June 27, 2022

Comments

  • KevinM
    KevinM almost 2 years

    I need help to only allow one decimal point to be entered into a EditText view. An example: I don't want 123.45.2 or 123..452 to be allowed. Once one decimal point is entered, no more are allowed.

  • user924
    user924 over 2 years
    and? you still can print a lot of dots
  • user924
    user924 over 2 years
    .123 still be allowed
  • Hussien Fahmy
    Hussien Fahmy over 2 years
    Api requirements is level 26