Record every word in the text editor when typing

I want to write every word in a text editor during input.

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 use

 mName.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS); 

But the problem is that it does not work on any device. Ex tablet LG. So I decided to do it programmatically.

Therefore, I use the 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); } 

This is where my application gets crash. I found a problem during debugging. The problem is that onTextChanged does not stop. So that it runs continuously. Someone help me solve my problem. I want to use every word. Offer any solutions.

+5
source share
4 answers

The reason your code freezes is because onChangeListener starts as soon as you setText .

You need to find a way to stop the launch if the text is the same.

To do this, you can use the temp line to store the prev value and compare each time the event fires. This will maintain a constant flow, not a loop.

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()); } 

Optimization of your code consists only in using only a sentence if the last letter entered is after a space. You can see how this is 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, the procedure described above exists. This will not take into account the case when you change the position of the cursor and enter text.

PS: you can use the getSelectionEnd() method and improve the method described above

+1
source

Do it in your xml design

 <EditText android:id="@+id/txtName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textCapWords" /> 

Link

EDIT

If you do not work, try this.

 android:capitalize="characters" 

to edit text in xml.

Link

0
source

Try the following:

Add the following to <EditText ../> in the XML:

 android:textAllCaps="true" 
0
source

There is an afterTextChanged event, you should try to use this method when your input has been temporarily completed.

0
source

Source: https://habr.com/ru/post/1244196/


All Articles