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.
<This solution uses Kotlin
and shows the number of characters left. Also, if the current number of characters surpasses the limit of 50, the text color will change to red.
Kotlin
private val mTitleTextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if(YOUR_EDIT_TEXT_ID.text.toString().trim().length < 51){
YOUR_CHAR_LEFT_TEXTVIEW_ID.text = (50 - YOUR_EDIT_TEXT_ID.text.toString().trim().length).toString()
YOUR_CHAR_LEFT_TEXTVIEW_ID.setTextColor(Color.BLACK)
}
else{
YOUR_CHAR_LEFT_TEXTVIEW_ID.text = "0"
YOUR_CHAR_LEFT_TEXTVIEW_ID.setTextColor(Color.RED)
}
}
override fun afterTextChanged(s: Editable) {}
}
Also, don't forget to add the TextWatcher
to your EditText
YOUR_EDIT_TEXT_ID.addTextChangedListener(mTitleTextWatcher)
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()));
}
};
Just set these 2 lines in TextInputLayout
in your XML file:
app:counterEnabled="true"
app:counterMaxLength="200"
Clear way;
abstract class CharacterWatcher : TextWatcher {
override fun afterTextChanged(text: Editable?) {
afterCharacterChanged(text?.lastOrNull(), text?.length)
}
override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, before: Int) {}
override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {}
abstract fun afterCharacterChanged(char: Char?, count: Int?)
}
editText.addTextChangedListener(new CharacterWatcher() {
@Override
public void afterCharacterChanged(@Nullable Character character, @Nullable Integer count) {
action()
}
});
You can do it with TextInputLayout
and compat 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>
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");
}
});