allow only number and period(.) in edit text in android-ThrowExceptions

Exception or error:

i want to allow only numbers and period(.) in a android edit text. How to do this .. can any body help?

How to solve:

EditText is derived from TextView which has a

void addTextChangedListener(TextWatcher watcher)

method. TextWatcher has callbacks, like

abstract void afterTextChanged(Editable s)

that you can implement in order to filter the new text to only contain numbers and period.


You should use the att:



i have set this two lines in the textedit from my your_class.xml


the first line is for setting the keybordart (only numbers and the dot).
the second line only allows predetermined characters.


Use android:inputType="numberDecimal".

EDIT: Saw that you later wrote that you want to use this as an editText for IP numbers. Don’t use my solution then. For “digits” (not numbers) plus the dot this won’t work.


Use android:numeric property.

Here an example


You need to make your EditText as numberSigned and numberDecimal. Add this line in your xml file –


Done 🙂


Currently, specific default input types (numeric, phone number) use KeyListener. You’ll just have to implement it to verify your data, and use setKeyListener in your EditText.


I found a tentative solution.

Click on the the EditText in the design view and then find the input property on the far right corner.

Select Number and textWebEditText. Albeit it won’t be the same as the view of the input type set for purely numbers, but it does away with process of having to press the number button at the bottom left of the keyboard every time.

Plus, unlike the EditText’s that have only the number property set and thus the ‘.’ period or decimal symbol is shown but cannot be selected, this input selection allows for the ‘.’ symbol to be selectable.

Bellow is the XML property:



Setting the numeric property to integer is not necessary to accomplish this, but I just included it as an extra setting to force it. I am only taking in an IP Address in the EditText so I only need numbers. If your solution needs to account for more than strictly integers, then I would not include that line and find another setting, perhaps decimal that would be more appropriate for your data.

Please note that because this solution makes it work and doesn’t necessarily force a format on the input, (ie–> the user can input a ‘.’ yes, but there is no limit to how many time the ‘.’ can be input–> thus this would cause an error when you take the input and use it for Socket or Web Programming. Further input checking and looping may be required before assigning the input to an IP address or something similar. Just putting that out there.

Leave a Reply

Your email address will not be published. Required fields are marked *