2

I want forcefully add first letter capital in EditText. So i can search about i get lots of solution but i Notice that user can shift and enter lower characters.

I tried below code :

android:inputType="textCapSentences|textCapWords"

Also I tried pragmatically :

EditText editor = new EditText(this); 
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

Any other solution. If user shift and enter lower characters it's automatically converted in to Upper characters.

Quick learner
  • 10,632
  • 4
  • 45
  • 55
Joker
  • 796
  • 1
  • 8
  • 24

6 Answers6

1

Try InputFilter here is working snipped. modify code acording your need.

EditText editText = (EditText) findViewById(R.id.editTextId);

editText..setFilters(new InputFilter[] { filter }); 

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(0))) { 
            return ""; 
        } 
    } 
    return null; 
} 
}; 
Vipul Prajapati
  • 1,183
  • 9
  • 11
1

Simple solution - Cannot restrict user to type first character capital so

When the user is done writing get the string value from edittext then change the first letter to capital and use that string.

ie On Button click or so get the string value from edittext

String content = edtEditText.getText().toString();
content.substring(0, 1).toUpperCase() + str.substring(1);
Quick learner
  • 10,632
  • 4
  • 45
  • 55
1

Hello Guys Below code is worked for me.

val toUpperCaseFilter =
        InputFilter { source, start, end, dest, dstart, dend ->
            val stringBuilder = StringBuilder()
            for (i in start until end) {
                var character = source[i]
                if(i==0 || source[i-1].equals(' ',true)) {
                    character = Character.toUpperCase(character!!) // THIS IS UPPER CASING
                }
                stringBuilder.append(character)
            }
            stringBuilder.toString()
        }

    binding.edtUsername.setFilters(arrayOf(toUpperCaseFilter))

This will work in every device and every keyboard.

  • This is the correct approach, but I found some issues though...When u are trying to write an email, all letters after the @ will be caps, which is not what we want...Use this kotlin code inside the for instead: var character = source[i] if (source.length == 1 && dest.isEmpty()) { character = Character.toUpperCase(character) } stringBuilder.append(character) – Ramiro G.M. Jan 13 '22 at 16:44
0

You just need to set android:capitalize="sentences" in your XML file, No need to set other "textCapWords" in android:inputType, as the latter seems to have priority over the first and the input will not be capitalized.

Sanjay Bhalani
  • 2,424
  • 18
  • 44
0

Try a text watcher like following

EditText et = (EditText)findViewById(R.id.editText);

    et.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

           if(count ==1){
              Character.toUpperCase(s.charAt(0));
           }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void afterTextChanged(Editable s) {}
    });
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • In if condition it's give me a error like : `Cannot resolve method 'toUpperCase(java.lang.CharSequence)'` – Joker Jul 04 '19 at 09:16
  • 1
    Try `Character.toUpperCase(s.charAt(0));` – Naitik Soni Jul 04 '19 at 09:21
  • Yes you are right , I missed that . I am editing my answer @NaitikSoni – Mithun Sarker Shuvro Jul 04 '19 at 09:23
  • 1
    This is not working when i shift and enter lower character. @MithunSarkerShuvro – Joker Jul 04 '19 at 09:26
  • This won't work because u are not assigning the upper case change to anything...if u try to assign it to your edittext inside the listener, u'll create an infinite loop...this is not a correct answer...look at Mayuresh Deshmukh's answer below...that's the correct approach... – Ramiro G.M. Jan 13 '22 at 16:42
0

You can use android:inputType="textCapWords" to capitalize every word and android:inputType="textCapSentences" for capitalising every sentence

Sadique Khan
  • 230
  • 3
  • 9