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
WonderKid Nov 29 '15 at 15:40 2015-11-29 15:40
source share