0

Folks,

I need to capitalize first letter of every sentence. I followed the solution posted here

First letter capitalization for EditText

It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.

What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.

Community
  • 1
  • 1
user3268403
  • 193
  • 1
  • 13
  • Are you storing your sentences in a string? If so you could try what was done [here](http://stackoverflow.com/questions/15259774/capitalise-first-letter-in-string-android) – aProperFox Jul 24 '14 at 18:02
  • 1
    1 - Capitalize the string. 2 - Assign it to the EditText – Phantômaxx Jul 24 '14 at 18:03
  • 1
    Use the answer here http://stackoverflow.com/questions/16078479/capitalize-first-word-of-a-sentence-in-a-string-with-multiple-sentences to capitalize the string you want to set. Then set that string to the view. The caps mode thing you tried is just a hint to the keyboard telling it how to interpret input. – Gabe Sechan Jul 24 '14 at 18:04

3 Answers3

2

The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.

If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.

Kevin Coppock
  • 128,402
  • 43
  • 252
  • 270
1

You can use substring to make this

private String capSentences( final String text ) {

    return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}
wbelarmino
  • 504
  • 3
  • 7
0

Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.

public static String capFirstLetter(String input) {
    return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}
Brett Haines
  • 186
  • 3
  • 14