views:

63

answers:

2

I want to restrict capital letter typing through keyboard on my form.

any one guide me how to achieve this?

+2  A: 

In source code:

InputFilter smallFilter = new InputFilter() {

  @Override
  public CharSequence filter(CharSequence source, int start, int end,
      Spanned dest, int dstart, int dend) {
    for (int i = start; i < end; i++) {
      if (Character.isUpperCase(source.charAt(i))) {
        char[] v = new char[end - start];
        TextUtils.getChars(source, start, end, v, 0);
        String s = new String(v).toLowerCase();

        if (source instanceof Spanned) {
          SpannableString sp = new SpannableString(s);
          TextUtils
              .copySpansFrom((Spanned) source, start, end, null, sp, 0);
          return sp;
        } else {
          return s;
        }
      }
    }
    return null;
  }
};

EditText vText = ...;
vText.setFilters(new InputFilter[]{smallFilter});

It is based on Android source of: InputFilter.AllCaps. Tested and works.

radek-k
I think he wants it the other way round: no capital letters
Maragues
both not working.above automatically sets keyboard to each time if you turn it off it allows you to enter 1 small character.and below one giving error InputFilter.AllCaps cannot be resolved.
UMMA
add"import android.text.InputFilter;"after package declaration.
radek-k
http://developer.android.com/reference/android/text/InputFilter.html
radek-k
I have edited my post and cleaned it up. Sorry for making a little mess.
radek-k
Don't appologise for trying to help :)
Tom Gullen
PS. Make sure add all neccessary imports (CTRL+SHIFT+O in Eclipse).
radek-k
Considered picking my answer as a correct one? ;)
radek-k
+1  A: 

First, what's your goal? Why do you want to restrict capital letters? Can't you take care of them on the validation of user input and use toLowerCase()

If you do need to restrict capital letters, the only way I can think of is overriding onTextChanged(CharSequence text, int start, int before, int after) and trying to do whatever you want inside it. I haven't tested it, but it might work.

Update: radek mentioned InputFilter. That solutions seems cleaner than mine, but I've never used them.

InputFilters can be attached to Editables to constrain the changes that can be made to them.

Maragues
There is no inputFilter or InputType (http://developer.android.com/reference/android/text/InputType.html) for what he's trying to do.
Macarse
thats the good idea :D
UMMA
Thanks for choosing my answer, but radek-k's is the better one. Anyway, we all learned something today ^^
Maragues
actually, let the user type whatever case he wants. we will convert it lower or upper case according to our need so then why to take care about restricting upper case thats why i like answer anyways both answers are good :)
UMMA