I want to capitilze every word in edit text while typing.
My XML:-
<com.project.edittext.AutoFitEditText android:id="@+id/txt_name" style="@style/scan_text_fields_white_bg" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="@string/validation_accept_all_except_special_char" android:fontFamily="sans-serif" android:imeOptions="flagNoExtractUi|actionDone" android:inputType="textNoSuggestions|textCapWords" android:maxLength="50" android:paddingBottom="3dp" android:paddingLeft="12dp" android:paddingRight="0dp" android:paddingTop="0dp" android:singleLine="true" android:textColor="@color/orange" />
Now I am using
mName.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
But problem is it is not working in some device. Ex LG tablet. So I decided to do this programatically.
So I am using following codes.
public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String mNameStr=s.toString(); String finalStr=""; if(!mNameStr.equals("")) { String[] strArray = mNameStr.split("[\s']"); if (strArray.length > 0) { for (int i = 0; i < strArray.length; i++) { finalStr+=capitalize(strArray[i]); } Logger.d("finalStr==> ", finalStr); } } mName.setText(finalStr.toString()); } private String capitalize(final String line) { return Character.toUpperCase(line.charAt(0)) + line.substring(1); }
Here my app getting crash. I found the problem while debugging. Problem is the onTextChanged is not getting stopped. So that it execute continuously. Anyone help me to resolve my problem. I want to capitalize every word. Please suggest any solutions.
Advertisement
Answer
The reason your code freezes is that the onChangeListener
is triggered as soon as you setText
.
You’ll have to find a way to stop trigering if the text is the same.
For this you can use a temp string to store the prev value and compare each time the event triggers. This will keep the flow to be consistent and not looping.
String prevString = "";
In afterTextChanged
String mNameStr = s.toString(); String finalStr = ""; if (!mNameStr.equals("")) { String[] strArray = mNameStr.split("[\s']"); if (strArray.length > 0) { int i = 0; for (i = 0; i < strArray.length - 1; i++) { finalStr += capitalize(strArray[i]) + " "; } finalStr += capitalize(strArray[i]); Log.d("finalStr==> ", finalStr); } } if (!finalStr.equals(prevString)) { prevString = finalStr; editText.setText(finalStr.toString()); editText.setSelection(finalStr.length()); }
An optimization to your code is to only capitalize the sentence if the last entered letter is after a space. You can see it implemented here.
editText.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) { String string = s.toString(); Log.d("STRING", string + " " + prevString); if (string.equals(prevString)) { return; } else if (string.length() == 0) return; // 1st character else if (string.length() == 1) { prevString = string.toUpperCase(); editText.setText(string.toUpperCase()); editText.setSelection(string.length()); } // if the last entered character is after a space else if (string.length() > 0 && string.charAt(string.length() - 2) == ' ') { string = string.substring(0, string.length() - 1) + Character.toUpperCase(string.charAt(string.length() - 1)); prevString = string; editText.setText(string); editText.setSelection(string.length()); } } });
However, there is a down-side to the above method. This will not consider the case where in you change the cursor position and enter text.
PS: you can use getSelectionEnd()
method and enhance the above method