Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capitalize every letter in an Android EditText?

Tags:

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?

like image 632
clavio Avatar asked Dec 04 '12 15:12

clavio


People also ask

How do you capitalize all letters in android?

If you are using an Android phone and Gboard, you can capitalize the first letter of any word with three touches. First, double-tap the word in question to highlight the word, then tap the shift button (the up arrow) on the keyboard to capitalize the first letter. Done!

How do you capitalize TextView in android?

Update. You can now use textAllCaps to force all caps. One of the answers on the linked question suggests 'android:textAllCaps="true"' This worked for me. You can do that using "textAllCaps" attribute, hence the downvote.

How can the user set caps to the text while reading input?

Use keyup() method to trigger the keyup event when user releases the key from keyboard. Use toLocaleUpperCase() method to transform the input to uppercase and again set it to the input element.


Video Answer


1 Answers

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.)

like image 89
tiktak Avatar answered Sep 21 '22 13:09

tiktak