Edittext with Number Keypad by Default, But Allowing Alphabetic Characters

Compose TextField with number keypad by default, but allowing alphabetic characters

There is no straightforward solution. So, I decided to implement it myself:

var keyboardType by remember { mutableStateOf(KeyboardType.Number) }
var text by rememberSaveable { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = keyboardType),
trailingIcon = {
if (keyboardType == KeyboardType.Number) {
Icon(
painterResource(
id = if (keyboardType == KeyboardType.Number)
R.drawable.ic_abc_24
else
R.drawable.ic_pin_24
),
contentDescription = "Change keyboard",
modifier = Modifier.clickable {
keyboardType = if (keyboardType == KeyboardType.Number)
KeyboardType.Text
else
KeyboardType.Number
}
)
}
}
)

How do I default to numeric keyboard on EditText without forcing numeric input?

Add the following line of code, and it will do the trick :)

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

This will show the the numeric keypad first, but also allows you to enter free text.

More information here.

Android EditText force numeric keyboard but allow non-numeric chars

This will keep it numeric but will allow the $ character, add any other special chars you need to the android:digits attribute

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="0123456789,$">

Android Edit text should only accepts numbers not emoticons, special icons and alphabets (By Typing or By Copying from Other apps)

Just use the input type as number it will not allow to paste anything.

<EditText
android:id="@+id/edtEnterAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/enter_amount_margin"
android:textSize="@dimen/header_three"
android:singleLine="true"
android:maxLength="8"
android:inputType="numberDecimal"
android:imeOptions="actionDone"
android:hint="Enter amount"
android:textColorHint="@color/gray_two" />

OR

 android:inputType="number"

OR

android:digits="0123456789"

OR

    edtEnterAmount.setOnFocusChangeListener(this);
InputFilter filter = new InputFilter() {
final int maxDigitsBeforeDecimalPoint=8;
final int maxDigitsAfterDecimalPoint=2;

@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"(([1-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"

)) {
if(source.length()==0)
return dest.subSequence(dstart, dend);
return "";
}

return null;

}
};

edtEnterAmount.setFilters(new InputFilter[] { filter });

Hope this help.Happy coding.

How to restrict the EditText to accept only alphanumeric characters

In the XML, add this:

 android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "


Related Topics



Leave a reply



Submit