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
Android Gridlayout with Dynamic Number of Columns Per Row
Get Text from Web Page to String
Permission Is Only Granted to System App
Android: Speech Recognition Without Using Google Server
How to Pass Integer from One Activity to Another
Best Place to Close Database Connection
How to Show a Dialog to Confirm That the User Wishes to Exit an Android Activity
Get Altitude by Longitude and Latitude in Android
Playing a Video in Videoview in Android
Retrieving All Drawable Resources from Resources Object
How to Create and Read Log on Android Devices
How to Autoplay HTML5 Mp4 Video on Android
Android Background Image Size in Pixel
Manifest Merger Fails for Appcomponentfactory
Does Alarm Manager Persist Even After Reboot
Listview Scroll to the End of the List After Updating the List