4

I have an edittext and I want any character that I type in edittext show in Capital letter. I have used this:

edittext.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

But it is not working. I want to do it dynamically. Any ideas.

rizzz86
  • 3,862
  • 8
  • 35
  • 52

7 Answers7

12

You just need to try with android:inputType attribute.

As per your requirement, you can include android:inputType="textCapCharacters"

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
9

Type of hack,Try this:

edittext.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {            

    }
        @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {             
    }
    @Override
    public void afterTextChanged(Editable arg0) {
          String s=arg0.toString();
      if(!s.equals(s.toUpperCase()))
      {
         s=s.toUpperCase();
         edittext.setText(s);
       edittext.setSelection(s.length());
      }
    }
});     

This should work(don't have time to check)try putting the lines in onTextChanged in afterTextChanged if this doesn't work..

ADM
  • 20,406
  • 11
  • 52
  • 83
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • Thanks Hiral. I will try this one. It seems interesting. – rizzz86 Feb 22 '12 at 08:34
  • Well it is returning StackOverflowError. And it has to because setting "edittext.setText(s);" will again call the onTextChangeListener. – rizzz86 Feb 22 '12 at 10:46
  • Then try putting it in afterTextChanged and remove from onTextChanged! – Hiral Vadodaria Feb 22 '12 at 10:50
  • @rizzz86: please see my edited code in answer.it will convert whole string in uppercase even though user enters it in lower case. – Hiral Vadodaria Feb 22 '12 at 11:12
  • 1
    Thanks @Hiral it works now. Added one more line: "edittext.setSelection(edittext.getText().length());" for character sequence. – rizzz86 Feb 22 '12 at 11:32
  • @hardPass: means? where is the problem? – Hiral Vadodaria Jul 16 '12 at 04:36
  • Oh, it works now. I just saw this in comment. edittext.setSelection(edittext.getText().length()); – hardPass Jul 16 '12 at 08:34
  • @Hiral I think it will convert when user try to write on field but letter will be showing in small case. Is there is way to show only capital letter on soft keyboard? – kamil Feb 23 '13 at 11:31
  • It works fine practically . No Issues except we should make the string reverse Thank you – not 0x12 May 21 '13 at 05:58
  • For me, none of the overrides in the TextWatcher were called. Possibly due to `The documentation states that the key events will only be propagated for the hardware key strokes, not software.`. in https://stackoverflow.com/questions/24425838/edittext-onkeylistener-not-working – Stephen Hosking May 14 '18 at 07:59
3

For doing it dynamically:

edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
Juan Sánchez
  • 980
  • 2
  • 10
  • 26
1
   EditText txt = FindViewById<EditText>(Resource.Id.textView1);
   txt.SetFilters(new Android.Text.IInputFilter[] {
                      new Android.Text.InputFilterAllCaps()
                  });
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
Ravikumar
  • 11
  • 2
1

Try this in your editText android:capitalize="characters"

Rakshi
  • 6,796
  • 3
  • 25
  • 46
1
<EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:capitalize="characters"/>
Vinit ...
  • 1,409
  • 10
  • 37
  • 66
-1

you can try this too, if nothing else works

String text=editText.getText().toString();
editText.setText(text.toUpperCase());

add this code to the onTextChangeListener of EditText

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184