I was wondering what the best way to do a live character count of an edit-text box is in Android. I was looking at this but I couldn\'t seem to make any sense of it.
<I met the same problem and I tried Cameron's method. It works but there is a minor bug: If the user use copy and paste then it fails to count the chars. So I suggest to do after the text changed, like 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()));
}
};
you can use a 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 set the TextWatcher for the edittext with
mEditText.addTextChangedListener(mTextEditorWatcher);
You can do character counting from xml itself using TextInputLayout wrapper for EditText introduced in SupportLibrary v23.1
Just wrap your EditText with a TextInputLayout and set CounterEnabled to true and Set a 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'll get a material effect like this
You may use counterOverflowTextAppearance , counterTextAppearance to style the counter.
EDIT
From Android documentation.
The TextInputEditText class is provided to be used as a child of this layout. Using TextInputEditText allows TextInputLayout greater control over the visual aspects of any text input. An example usage is as so:
<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
Use android:maxLength="140"
That should work. :)
Hope that helps
try this
private TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
editText.post(new Runnable() {
@Override
public void run() {
if (length < 100) {
if (count > 0 && after <= 0)/*remove emoij*/ {
length--;
} else if (count > after)/*remove text*/ {
length--;
} else if (count == 0 && after > 1)/*emoij*/ {
++length;
} else if (count == 0 && after == 1)/*Text*/ {
++length;
} else if (count > 0 && after > 1) {
++length;
}
if (s.length() <= 0)
length = 0;
Log.w("MainActivity", " Length: " + length);
} else {
if (count > 0 && after <= 0)/*remove emoij*/ {
length--;
} else if (count > after)/*remove text*/ {
length--;
}
Log.w("MainActivity", " Length: " + length);
}
if (length == 100) {
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(s.length())});
} else {
editText.setFilters(new InputFilter[]{});
}
}
});
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
`
Its very Simple Follow the instructions below:
====Add them to your Imports===
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);