Вы находитесь на странице: 1из 4

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

Limit the Length of TextView [duplicate]

android

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" />

share

improve this question

user1737894

Dipak Keshariya
14.4k 9 50 102

Asked
Oct 29 '12 at 11:04
Edited
Oct 29 '12 at 11:14

marked as duplicate by Dharmendra, RivieraKid, BNL, Ted Hopp, Ahmad Oct 29 '12 at 15:10
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

25

Order By

Votes

Use android:maxLength="12" for limit of 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 other property like as


android:ellipsize="end"
android:singleLine="true"

using this property "..." will be added end of the text like
"Hello How are ..." instead of "Hello How are you?"

share

improve this answer


Niranj Patel
25.6k 10 68 101

Answered
Oct 29 '12 at 11:07

Edited
Oct 29 '12 at 11:13

line 1: "hello how are" line 2 :"you" instaed of hello how are you Prasad Oct 15 '14 at 13:04
add a comment

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"/>

share

improve this answer


Vipul Shah
19.5k 6 43 63

Answered
Oct 29 '12 at 11:09

Dipak Keshariya
14.4k 9 50 102

Edited
Oct 29 '12 at 11:14

android:ellipsize="end" add a "..." not "marequee". stackoverflow.com/a/20511174/2469427 GeneralKimi Dec 18 '14 at 8:56
add a comment

add code like


android:maxLength="12"

share

improve this answer


Devangi Desai
1,007 7 15

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

Answered
Oct 29 '12 at 11:06

<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" />

share

improve this answer


Nermeen
12.5k 4 33 47

Answered
Oct 29 '12 at 11:07

Add the following max length parameter to your text viewandroid:maxLength="12"

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

share

improve this answer


Ravi
823 2 9 26

Answered
Oct 29 '12 at 11:32

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Вам также может понравиться