Live Character Score for EditText

I was wondering what is the best way to make a live number of text field characters in Android. I looked at this , but I could not understand anything.

To describe the problem, I have an EditText, and I'm trying to limit the characters to 150. I can do this with an input filter, however I want to show the number of characters that the user has right under the text box (almost like a stack overflow is running right now).

If someone can write a small piece of example code or point me in the right direction, I would really appreciate it.

+67
android
Jun 10 '10 at 11:08
source share
10 answers

you can use TextWatcher to see when the text has changed

private TextView mTextView; private EditText mEditText; private final TextWatcher mTextEditorWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { //This sets a textview to the current length mTextView.setText(String.valueOf(s.length())); } public void afterTextChanged(Editable s) { } }; 

you install TextWatcher for edittext with

 mEditText.addTextChangedListener(mTextEditorWatcher); 
+125
Dec 15 '10 at
source share

You can do character counting from xml itself using the TextInputLayout wrapper for EditText, introduced in SupportLibrary v23.1

Just wrap your EditText with TextInputLayout and set the CounterEnabled parameter to true and set counterMaxLength.

 <android.support.design.widget.TextInputLayout android:id="@+id/textContainer" android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="20" > <EditText android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Text Hint" /> </android.support.design.widget.TextInputLayout> 

You will get a material effect, for example

You can use counterOverflowTextAppearance , counterTextAppearance to style the counter.

EDIT

From the documentation for Android.

The TextInputEditText class is intended to be used as a child of this layout. Using TextInputEditText allows TextInputLayout to have more control over the visual aspects of any text input. An example of use is as follows:

  <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/form_username"/> </android.support.design.widget.TextInputLayout> 

TextInputLayout TextInputEditText

+79
Nov 29 '15 at 15:40
source share

Its very simple. Follow the instructions below:

==== Add them to your Import ===

 import android.text.Editable; import android.text.TextWatcher; 

===== Define this =====

 private TextView sms_count; 

=========== Inside On Create =====

 sms_count = (TextView) findViewById(R.id.textView2); final TextWatcher txwatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { sms_count.setText(String.valueOf(s.length())); } public void afterTextChanged(Editable s) { } }; sms_message.addTextChangedListener(txwatcher); 
+5
Apr 09 2018-11-11T00:
source share
  You can use TextWatcher class to see text has changed and how much number of character remains.Here i have set counter of 140 characters. EditText typeMessageToPost; TextView number_of_character; public void onCreate(Bundle savedBundleInstance) { super.onCreate(savedBundleInstance); setContentView(R.layout.post_activity); typeMessageToPost.addTextChangedListener(mTextEditorWatcher); } private final TextWatcher mTextEditorWatcher=new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub number_of_character.setText(String.valueOf(140-s.length())); } }; 
+4
Sep 11 '14 at 5:32
source share

You can do this with TextInputLayout and compatible libraries with:

 app:counterEnabled="true" app:counterMaxLength="420" 

and complete:

 <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:counterEnabled="true" app:counterMaxLength="420"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLength="420" /> </android.support.design.widget.TextInputLayout> 
+4
Aug 18 '17 at 22:40
source share

Just set these 2 lines in TextInputLayout in the XML file:

 app:counterEnabled="true" app:counterMaxLength="200" 
+3
Jan 16 '17 at 13:49 on
source share

I met the same problem and I tried Cameron's method. It works, but there is a minor error: if the user uses copy and paste, then he cannot read the characters. Therefore, I propose to do it after changing the text, as shown below:

  private final TextWatcher mTextEditorWatcher = new TextWatcher() { public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { } public void afterTextChanged(Editable s) { //This sets a textview to the current length mTextView.setText(String.valueOf(s.length())); } }; 
+1
Mar 25 '15 at 10:31
source share

in xml add this attribute for editText

  android:maxLength="80" 

in java add this listener

  ed_caption.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) { tv_counter.setText(80 - s.toString().length() + "/80"); } }); 
+1
May 22 '17 at 11:03 a.m.
source share

Try to do something like this.

This solution may be more efficient, unlike getting CharSequence.length. Each time you press the soft keyboard, an event fires; therefore, if you make a length, it will count CharSequence each time, which may slow down if you start getting large CharSequnces. When listening to events, when the text changes, counting occurs before and after the count. This works well for increase and decrease values.

 @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { int tick = start + after; if(tick < mMessageMax) { int remaining = mMessageMax - tick; ((TextView)findViewById(R.id.contact_us_chars)).setText(String.valueOf(remaining)); } } 
0
Sep 24 '13 at 15:42
source share

Use android: maxLength = "140"

That should work. :)

Hope that helps

0
Oct. 16 '16 at 7:22
source share



All Articles