clavio
clavio

Reputation: 1052

How to capitalize every letter in an Android EditText?

I have an array of editTexts which I make like this:

        inputs[i] = new EditText(this);
        inputs[i].setWidth(376);
        inputs[i].setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
        tFields.addView(inputs[i]);

I want every character to be capitalized. Right now, the first letter of the first word is lowercase, then all the characters afterwords are upper case. I can take what the user inputs after they are done and convert that to uppercase, but that's not really what I'm going for. Is there a way to get these fields to behave the way I want them to?

Upvotes: 27

Views: 47643

Answers (11)

sanjay rathore
sanjay rathore

Reputation: 1

Most of people have already answered about how to capitalize the editTexts. I have same requirement in my current project but with slight change. I need to make it uppercase if it's lowercase i.e., user typed 'a' then it need to be changed to 'A'.

AddTextChangeListener comes to the rescue. Someone has already covered it above but it falls in infinite loop and app crashes. There is just small modification required to make it work. so let's start.

Kotlin:

val watcher: TextWatcher = object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        editText.removeTextChangedListener(this)
        editText.setText(s.toString().toUpperCase())
        editText.setEditTextSelection(editText.text.length)// Required to make cursor go to last index otherwise it would always point to start index
        editText.addTextChangedListener(this)
    }

    override fun afterTextChanged(s: Editable) {}
}

editText.addTextChangedListener(watcher)

Upvotes: 0

Mariusz Wiazowski
Mariusz Wiazowski

Reputation: 2446

All you have to do is set the correct input type for the EditText view:

editText.inputType == InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS

As an alternative you can use an InputFilter. Here I have created a Kotlin extension:

fun EditText.allCaps() {
    val newFilters = this.filters.copyOf(this.filters.size + 1)

    newFilters[newFilters.size - 1] = InputFilter.AllCaps()

    this.filters = newFilters
}

I prefer to use both of this methods. Please note that InputFilter works also for hardware keyboard, on the other hand InputType will be effective only for soft keyboard.

Upvotes: 1

Ricardo
Ricardo

Reputation: 9696

I know this question is a bit old but here is explained how to do it in different ways:

Applying UpperCase in XML

Add the following to the EditText XML:

android:inputType="textCapCharacters"

Applying UpperCase as the only filter to an EditText in Java

Here we are setting the UpperCase filter as the only filter of the EditText. Notice that this method removes all the previously added filters.

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

Adding UpperCase to the existing filters of an EditText in Java

To keep the already applied filters of the EditText, let's say inputType, maxLength, etc, you need to retrieve the applied filters, add the UpperCase filter to those filters, and set them back to the EditText. Here is an example how:

InputFilter[] editFilters = editText.getFilters();
InputFilter[] newFilters = new InputFilter[editFilters.length + 1];
System.arraycopy(editFilters, 0, newFilters, 0, editFilters.length);
newFilters[editFilters.length] = new InputFilter.AllCaps();  
editText.setFilters(newFilters);

Upvotes: 12

indrajeet jyoti
indrajeet jyoti

Reputation: 431

To capitalize the first word of each sentence use :

android:inputType="textCapSentences"

To Capitalize The First Letter Of Every Word use :

android:inputType="textCapWords"

To Capitalize every Character use :

android:inputType="textCapCharacters"

Upvotes: 3

med116
med116

Reputation: 1616

For some reason adding the xml did not work on my device.

I found that a Text Filter works for to uppercase, like this

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

    InputFilter toUpperCaseFilter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {

            StringBuilder stringBuilder = new StringBuilder();

            for (int i = start; i < end; i++) {
                Character character = source.charAt(i);
                character = Character.toUpperCase(character); // THIS IS UPPER CASING
                stringBuilder.append(character);

            }
            return stringBuilder.toString();
        }

    };

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

Upvotes: 0

Douglas Santos
Douglas Santos

Reputation: 103

You can try this:

android:inputType="textCapCharacters"

It worked for me. =)

Upvotes: 6

jzeferino
jzeferino

Reputation: 7850

This will make all the characters uppercase when writing.

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

Original answer here.

Upvotes: 21

Pihu
Pihu

Reputation: 1039

try using this single line of code in your xml:

android:capitalize="sentences"

Upvotes: 0

tiktak
tiktak

Reputation: 1230

You need to tell it it's from the "class" text as well:

inputs[i] = new EditText(this);
inputs[i].setWidth(376);
inputs[i].setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
tFields.addView(inputs[i]);

The input type is a bitmask. You can combine the flags by putting the | (pipe) character in the middle, which stands for the OR logic function, even though when used in a bitmask like this it means "this flag AND that other flag".

(This answer is the same as Robin's but without "magic numbers", one of the worst things you can put in your code. The Android API has constants, use them instead of copying the values and risking to eventually break the code.)

Upvotes: 24

Robin Chander
Robin Chander

Reputation: 7425

This would work

    inputs[i] = new EditText(this);
    inputs[i].setWidth(376);
    inputs[i].setInputType(0x00001001);
    tFields.addView(inputs[i]);

0x00001001 corresponds to "textCapCharacters constant.

Upvotes: 0

Houcine
Houcine

Reputation: 24181

Just use String.toUpperCase() method.

example :

String str = "blablabla";
editText.setText(str.toUpperCase()); // it will give : BLABLABLA

EDIT :

add this attribute to you EditText Tag in your layout xml :

 android:textAllCaps="true"

OR:

If you want whatever the user types to be uppercase you could implement a TextWatcher and use the EditText addTextChangedListener to add it, an on the onTextChange method take the user input and replace it with the same text in uppercase.

editText.addTextChangedListener(upperCaseTextWatcher);

final TextWatcher upperCaseTextWatcher = new TextWatcher() {

public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    editText.setText(editText.getText().toString().toUpperCase());
    editText.setSelection(editText.getText().toString().length());
}

public void afterTextChanged(Editable editable) {
}

};

Upvotes: 6

Related Questions