i have the following problem to resolve in an Android App. I have an editText which has to show only numbers and the the letters 'x' and 'c' when the keyboard is prompted. Is this possible? Thanks for the help!
Sure you can, with filters using InputFilter.
Here a piece of sample code:
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.isDigit(source.charAt(i)) || (source.charAt(i) == 'x') || (source.charAt(i) == 'c'))
{
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });
You have to build your own keyboard or you can restrict input in such a way:
<EditText
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,xc" />
try below properties for your EditText
Example :
Alphabet
android:inputType="text" // for alphabet
you can put your own combination of digits
android:digits="0,1,2,3,4,5,6,7,8,9,*,xc" // you can put your own combination of digits
Alphanumeric
android:digits="0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Numeric
input.setRawInputType(Configuration.KEYBOARD_12KEY); // its show only the numeric keyboard.
Related
I have to restrict characters for a particular EditText. For that, I am using
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " in XML but if I use this I am not able to get next button on the soft keyboard in spite of me showing android:imeOptions="actionNext". It is always shows done in soft keyboard. So I removed the digits and I am using android:inputType="textCapCharacters" in XML and want to use INPUT FILTERS to restrict the characters programmatically. How do I do that?
Is it possible? if so how to use INPUT FILTERS to restrict only "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "?
Try this one
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if(source.length() > 10) return "";
else{
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i)) && !Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
}
return null;
}
};
Then set it to you editext
myEditxt.setFilters(new InputFilter[] { filter });
Add this in string.xml
<string name="my_regex">ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789</string>
In XML :
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="#string/my_regex"
/>
I cannot enter number (numeric) in the Edittext field.
If I keep android:inputType="text", number cannot be entered.
If I keep android:inputType="text|number", the keyboard accepts only digits.
But If I keep android:inputType="textMultiLine", I can enter both text/number but the first character cannot be number and it should be a character.
And I tried doing with other options too, nothing worked. I'm building the application with the target sdk:21
Note: I need both text / number as input
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.isDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
your_edit_text.setFilters(new InputFilter[]{filter});
public boolean isLeadingDigit(final String value){
final char c = value.charAt(0);
return (c >= '0' && c <= '9');
}
You can use
android:inputType="numberDecimal"
Try to not specify inputType or set it to "none" if you want to be able to enter text and numbers.
I have a TextView in my app that i want a user to be able to only enter alpha-numeric characters in. How can this be done? Thanks!
In the XML, put this:
android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
Here is a better solution......... https://groups.google.com/forum/?fromgroups=#!topic/android-developers/hS9Xj3zFwZA
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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
The InputFilter solution works well, and gives you full control to filter out input at a finer grain level than android:digits. The filter() method should return null if all characters are valid, or a CharSequence of only the valid characters if some characters are invalid. If multiple characters are copied and pasted in, and some are invalid, only the valid characters should be kept (#AchJ's solution will reject the entire paste if any characters a invalid).
public static class AlphaNumericInputFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// Only keep characters that are alphanumeric
StringBuilder builder = new StringBuilder();
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (Character.isLetterOrDigit(c)) {
builder.append(c);
}
}
// If all characters are valid, return null, otherwise only return the filtered characters
boolean allCharactersValid = (builder.length() == end - start);
return allCharactersValid ? null : builder.toString();
}
}
Also, when setting your InputFilter, you must make sure not to overwrite other InputFilters set on your EditText; these could be set in XML, like android:maxLength. You must also consider the order that the InputFilters are set. When used in conjunction with a length filter, your custom filter should be inserted before the length filter, that way pasted text applies the custom filter before the length filter (#AchJ's solution will overwrite all other InputFilters and only apply the custom one).
// Apply the filters to control the input (alphanumeric)
ArrayList<InputFilter> curInputFilters = new ArrayList<InputFilter>(Arrays.asList(editText.getFilters()));
curInputFilters.add(0, new AlphaNumericInputFilter());
InputFilter[] newInputFilters = curInputFilters.toArray(new InputFilter[curInputFilters.size()]);
editText.setFilters(newInputFilters);
This should work:
textView.setInputType(InputType.TYPE_CLASS_NUMBER);
I have an EditText on which I would like to let user enter only non-numeric chars (say A-Z or a-z): is there a way to do it? All the combinations I used (text, textPersonName and so on) let the user select also numbers.
I think you have to write your own InputFilter and add it to the set of filters for the EditText. Something like this might work:
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))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
My application takes userid from user as input, the userid is alphanumeric i.e just the first character is (a-z), other part is numeric. How can I validate input of this type ( like G34555) ?
Use a regex. This should do it assuming the first letter can be upper or lower case:
Pattern p = Pattern.compile("[a-zA-Z][0-9]+");
Matcher m = p.matcher("some text you want");
boolean isAlphaNum = m.matches();
http://osdir.com/ml/Android-Developers/2009-11/msg02501.html seems like a more decent solution, it does not allow entering the chars that are not accepted.
Code from link:
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.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});
I have resolved issue by using simple string function matches
String str="mystring";
str.matches("[a-zA-Z][0-9]+");