Set Edittext Digits Programmatically

Set EditText Digits Programmatically

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.

How to user android:digits programmatically

Try the code below

edtTxt.setInputType(InputType.TYPE_CLASS_TEXT);
edtTxt.setFilters(new InputFilter[]{
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
if (src.equals("")) {
return src;
}
if (src.toString().matches("[a-zA-Z ]+")) {
return src;
}
return "";
}
}
});

Change android:digits programmatically

Adding

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

after

manual_ip.setInputType(InputType.TYPE_CLASS_PHONE);

and nothing after

manual_ip.setInputType(InputType.TYPE_CLASS_TEXT);

solves my problem!

Set dynamically digits for EditText - Android

Solution

editText.keyListener = DigitsKeyListener.getInstance(Util.ENGLISH_LANGUAGE_DIGITS)
editText.setRawInputType(InputType.TYPE_CLASS_TEXT)

Android - How to set numeric EditText to a specified number of digits

Programmatically.

Also, you can save time by using existing library EditText with validation. You can use a regex type with following pattern [0-9]{8}.

Edittext only allow letters (programmatically)

You can use this code below:

InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(source.charAt(i))&&!Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[] { filter });

Android: Edittext that only allows a digit from 1-9

Ther have two ways for your question
1) in your XML fayl

<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="123456789"
android:inputType="number"
android:maxLength="1"/>

2) in your Activity class

    mEdit.setInputType(InputType.TYPE_CLASS_NUMBER );
mEdit.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
mEdit.setSingleLine(true);

How to set InputType 'time' programmatically?

For time field:

 setInputType(InputType.TYPE_CLASS_DATETIME |InputType.TYPE_DATETIME_VARIATION_TIME);

datetime has dot(.), slash(/) which I do not want to show in keyboard

You should try with

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


Related Topics



Leave a reply



Submit