android:maxLength - Set the Maximum Length of a TextView

How to Set the Maximum Length of a TextView

android:maxLength was add in API Level 1

Set an input filter to constrain the text length to the specified number. It must be an integer.

1. Add a TextView in the main.xml file.

2. In the main.xml file, add the code below.

<TextView
        ...
android:maxLength = "10"
/>

3. If in the <TextView the android:layout_width is set to:
  • wrap_content, the text and the whole TextView will stop at the maxLength of "10". 
  • match_content, the text will stop at maxLength of "10", but the TextView will span the full width of the View the TextView inside.
4. Compile and run!

Related Articles:

Resources:
http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLength
http://developer.android.com/reference/android/R.attr.html#maxLength