Getting the number of characters in an EditText

I am trying to get the number of characters in an EditText. I studied the different properties of the EditText and TextView classes, but there seems to be no function that returns the number of characters. I tried to use TextWatcher, but this is not ideal, because sometimes I load the saved message into EditText from the settings, and TextWatcher does not take into account characters that were not entered directly.

Any help would be great!

Hooray!

+6
java android android-edittext
source share
2 answers

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

int length = editText.getText().length(); 
+23
source share
 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); 
+10
source share

All Articles