Amit Ranjan
Amit Ranjan

Reputation: 83

How to hide number showing on google softkeyboard programmatically

I need one help , I want to hide number from google softkeyboard programmatically and want only letter on CapsLock, as you can see this link http://www.androidcentral.com/how-add-dedicated-number-row-google-keyboard,it's showing number above the keyboard , that i don't want.

I tried :

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()}); // for making capslock on

<EditText
               android:id="@+id/firstName"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:inputType="textCapCharacters"
               android:maxLength="10"/>

But Still the number is showing and user is able to type the number also, which i don't need, I need user can type only letter A to Z

I Found one Solution where user can't able to type number , but it's not showing the capital letter (Caps ON on softkeyboard).

Here is the solution and it's worked for me now : Thanks EveryOne for your support.

edittext.setFilters(new InputFilter[] {
    new InputFilter() {
        public CharSequence filter(CharSequence src, int start,
                int end, Spanned dst, int dstart, int dend) {
            if(src.equals("")){ // for backspace
                return src;
            }
            if(src.toString().matches("[A-Z ]+")){
                return src;
            }
            return "";
        }
    }
});
<EditText
               android:id="@+id/firstName"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:inputType="textCapCharacters"
               android:maxLength="10"/>

Upvotes: 2

Views: 842

Answers (2)

Garg
Garg

Reputation: 2731

First Way:- Set android:inputType="textCapSentences" on your EditText.

but this will only work If your device keyboard Auto Capitalize Setting enabled.

Second Way:- Can use InputFilters programmatically

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

UPDATE ANSWER: Try with add this line in your EditText tag.

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

hope this will work for you.

Upvotes: 1

Nazim ch
Nazim ch

Reputation: 854

Example for a validator for validate that input string is of alphabets alone

 import java.util.regex.Matcher;
 import java.util.regex.Pattern;


public class AlphabetValidator {

private Pattern pattern;
private Matcher matcher;

public static final String ALPHABET_PATTERN = "^[a-zA-Z]+[\\p{L} .'-]*$";

public AlphabetValidator() {
    pattern = pattern.compile(ALPHABET_PATTERN);
}

/**
 * Validate hex with regular expression
 *
 * @param hex hex for validation
 * @return true valid hex, false invalid hex
 */
public boolean validate(final String hex) {

    matcher = pattern.matcher(hex);
    return matcher.matches();
}
}

in java file include

  private AlphabetValidator alphabetValidator;

  //after submission check

  if (!alphabetValidator.validate(fieldname.getText().toString())) {
                        fieldname.setError("Error message");
                        fieldname.requestFocus();
                    }

Upvotes: 0

Related Questions