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
Try This Code, it will capitalize first character of all words.
- set addTextChangedListener for EditText view
edt_text.addTextChangedListener(watcher);
- Add TextWatcher
TextWatcher watcher = new TextWatcher() {
int mStart = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String capitalizedText;
if (input.length() < 1)
capitalizedText = input;
else if (input.length() > 1 && input.contains(" ")) {
String fstr = input.substring(0, input.lastIndexOf(" ") + 1);
if (fstr.length() == input.length()) {
capitalizedText = fstr;
} else {
String sstr = input.substring(input.lastIndexOf(" ") + 1);
sstr = sstr.substring(0, 1).toUpperCase() + sstr.substring(1);
capitalizedText = fstr + sstr;
}
} else
capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
if (!capitalizedText.equals(edt_text.getText().toString())) {
edt_text.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
edt_text.setSelection(mStart);
edt_text.removeTextChangedListener(this);
}
});
edt_text.setText(capitalizedText);
}
}
};
If you want capital first letter in every word then use android:inputType="textCapWords"
For Better understanding
<EditText
android:id="@+id/edt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"/>
And if you capital word for every sentence then use it .
android:inputType="textCapSentences"
this line in your xml. I mean in your EditText.
For Better understanding
<EditText
android:id="@+id/edt_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences"/>
For Capitalisation in EditText you can choose the below two input types:
- android:inputType="textCapSentences"
- android:inputType="textCapWords"
textCapSentences
This will let the first letter of the first word as Capital in every sentence.
textCapWords This will let the first letter of every word as Capital.
If you want both the attributes just use | sign with both the attributes
android:inputType="textCapSentences|textCapWords"
Statically (i.e. in your layout XML file): set android:inputType="textCapSentences"
on your EditText
.
Programmatically: you have to include InputType.TYPE_CLASS_TEXT
in the InputType
of the EditText
, e.g.
EditText editor = new EditText(this);
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
Can be combined with text and its variations to request capitalization of the first character of every sentence.
- Google Docs
use this code to only First letter capitalization for EditText
MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="true">
</EditText>
</RelativeLayout>
MainActivity.java
EditText et = findViewById(R.id.et);
et.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
if (et.getText().toString().length() == 1 && et.getTag().toString().equals("true"))
{
et.setTag("false");
et.setText(et.getText().toString().toUpperCase());
et.setSelection(et.getText().toString().length());
}
if(et.getText().toString().length() == 0)
{
et.setTag("true");
}
}
public void afterTextChanged(Editable editable) {
}
});