Reputation: 3869
I have set android:inputType="text|textCapWords"
to my EditText. When I type something in the field, the first letter is correctly capitalised, but if I set a full capitalised (or full lowercase) text using the setText()
method, the text remains fully capitalised (or in lowercase).
How can I use setText for the text to comply with the input type?
Upvotes: 0
Views: 633
Reputation: 1
Use this
EditText.getText().clear();
EditText.getText().append("test");
rather than
EditText.setText("test");
so that the text that has been set follows the input type behavior
Upvotes: 0
Reputation: 1175
You can check for your editText Type constants, and add simple .toUpperCase()
to the text you're adding, like this:
if(mEditText.getInputType() == TYPE_TEXT_FLAG_CAP_CHARACTERS+ TYPE_CLASS_TEXT)
mEditText.setText("Some text".toUpperCase());
else
mEditText.setText("some text");
More for input types constants can be found here
Upvotes: 0
Reputation: 2893
As @Amarok suggests
This is expected behavior. You have to format the text yourself before using the setText() method
But if you want to format your text just like android:inputType="text|textCapWords
you can use the following method:
public static String modifiedLowerCase(String str){
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
for (String s : strArray) {
String cap = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
builder.append(cap + " ");
}
return builder.toString();
}
and use it like
textView.setText(modifiedLowerCase("hEllo worLd"));
It will convert it like :
Hello World
Upvotes: 1