Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting character count of EditText

I'm trying to get a character count of an EditText. I've looked into different properties of the EditText and TextView classes, but there doesn't seem to be a function that returns the amount of characters. I have tried to use the TextWatcher, but that is not ideal since sometimes I load a saved message into the EditText from the preferences, and TextWatcher does not count characters not typed right then.

Any help would be great!

Cheers!

like image 497
strange quark Avatar asked Jun 02 '10 19:06

strange quark


3 Answers

Just grab the text in the EditText as a string and check its length:

int length = editText.getText().length();
like image 168
Mark B Avatar answered Oct 15 '22 03:10

Mark B


EditText edittext;
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
           textview.setText(String.valueOf(s.length());
        }

        public void afterTextChanged(Editable s) {
        }
};
 editText.addTextChangedListener(mTextEditorWatcher);
like image 14
hunse Avatar answered Oct 15 '22 03:10

hunse


In Kotlin it's very easy

<EditText_name>.Text.length

like image 1
Lo-oker Avatar answered Oct 15 '22 02:10

Lo-oker