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
Creating Custom Lockscreen in Android
How to Crop an Image in Android
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
Android Gradle Apache Httpclient Does Not Exist
Android - Setonclicklistener VS Onclicklistener VS View.Onclicklistener
How to Always Run a Service in the Background
Googlesignatureverifier Signature Not Valid Message (Google Play Services 9.0.0)
Proguard Causing Runtimeexception (Unmarshalling Unknown Type Code) in Parcelable Class
Complete Working Sample of the Gmail Three-Fragment Animation Scenario
What Is the Purpose of Android's <Merge> Tag in Xml Layouts
Progressdialog Is Deprecated.What Is the Alternate One to Use
How to Get Available Wifi Networks and Display Them in a List in Android
How to Add "Go Back" Function in Webview Inside Fragment
How to Automatically Add Thousand Separators as Number Is Input in Edittext
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
How to Programmatically Force Bluetooth Low Energy Service Discovery on Android Without Using Cache