28

Possible Duplicate:
Limit text length of EditText in Android

I am a textView on Activity, which is being displayed as per the parameter it is receiving from the JSON response, I need to restrict it to 12 characters only.

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/includeheadersetting"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:text="Name"
    android:textColor="#000000"
    android:textStyle="bold" />
Community
  • 1
  • 1

5 Answers5

86

Use android:maxLength="12" to limit the text length

 <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/includeheadersetting"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="Name"
            android:maxLength="12"
            android:textColor="#000000"
            android:textStyle="bold" />

You can also use another property as follows:

android:ellipsize="end"
android:maxLines="1"

Using this property "..." will be added end of the text as follows:

"Hello How are ..." instead of "Hello How are you?"

Jay Atkinson
  • 3,279
  • 2
  • 27
  • 41
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
7

Generally speaking only including android:maxLength is not considered good idea.

Use maxLength attribute, then use the android:ellipsize="marquee" to add a "..." automatically to the end of any line that has been cut-off.

<TextView 
    android:id="@+id/txtView" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:maxLines="1" 
    android:maxLength="10" 
    android:ellipsize="marquee"/>
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Vipul
  • 27,808
  • 7
  • 60
  • 75
1

add code like

android:maxLength="12"
Devangi Desai
  • 1,355
  • 12
  • 17
1

Add the following max length parameter to your text view-

android:maxLength="12"

whatever the limit you want you can replace that like instead of 12 can give 14 or whatever length you want.

Ravi
  • 2,277
  • 3
  • 22
  • 37
0

Add android:maxLength="12" to your text view..

<TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/includeheadersetting"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="Name"
            android:textColor="#000000"
            android:textStyle="bold"
            android:maxLength="12" />
Nermeen
  • 15,883
  • 5
  • 59
  • 72