Set EditText Digits Programmatically

ryandlf picture ryandlf · Sep 4, 2011 · Viewed 72k times · Source

I am essentially trying to set the digits value of an EditText programmatically. So far I have:

weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
weightInput.setKeyListener(DigitsKeyListener.getInstance());

Which is fine, but I also want to be able to include a decimal place (.). Any ideas?

Answer

Whiler picture Whiler · Sep 4, 2011

Try this:

<EditText
    android:inputType="number"
    android:digits="0123456789."
/>

From Code:

weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));

But, it allows the user to include several "." See JoeyRA's answer for real numbers.