Search code examples
androidandroid-edittext

Set EditText Digits Programmatically


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?


Solution

  • 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.