I\'m working on a little personal todo list app and so far everything has been working quite well. There is one little quirk I\'d like to figure out. Whenever I go to add a
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
or android:inputType="textCapSentences"
will only work If your device keyboard Auto Capitalize Setting enabled.
if you are writing styles in styles.xml then
remove android:inputType property and add below lines
<item name="android:capitalize">words</item>
Apply following line in your EditText in XML.
android:inputType="textCapSentences|textMultiLine"
It will also allow multi-line support.
To capitalize, you can do the following with edit text:
To make first letter capital of every word:
android:inputType="textCapWords"
To make first letter capital of every sentence:
android:inputType="textCapSentences"
To make every letter capital:
android:inputType="textCapCharacters"
But this will only make changes to keyboard and user can change the mode to write letter in small case.
So this approach is not much appreciated if you really want the data in capitalize format, add following class first:
public class CapitalizeFirstLetter {
public static String capitaliseName(String name) {
String collect[] = name.split(" ");
String returnName = "";
for (int i = 0; i < collect.length; i++) {
collect[i] = collect[i].trim().toLowerCase();
if (collect[i].isEmpty() == false) {
returnName = returnName + collect[i].substring(0, 1).toUpperCase() + collect[i].substring(1) + " ";
}
}
return returnName.trim();
}
public static String capitaliseOnlyFirstLetter(String data)
{
return data.substring(0,1).toUpperCase()+data.substring(1);
}
}
And then,
Now to capitalize every word:
CapitalizeFirstLetter.capitaliseName(name);
To capitalize only first word:
CapitalizeFirstLetter.capitaliseOnlyFirstLetter(data);
I encountered the same problem, just sharing what I found out. Might help you and others...
Try this on your layout.add the line below in your EditText
.
android:inputType="textCapWords|textCapSentences"
works fine on me.. hope it works also on you...
In your layout XML file :
set android:inputType="textCapSentences"
On your EditText
to have first alphabet of the first word of each sentence as capital
Or android:inputType="textCapWords"
on your EditText to have first alphabet of each word as capital