Can I limit TextView's number of characters? - android

What I have now is a ListView of TextView elements. each TextView element displays a text (the text length varies from 12 words to 100+). What I want is to make these TextViews display a portion of the text (let's say 20 word or roughly 170 chars).
How to limit the TextView to a fixed number of characters?

Here is an example. I limit the sizewith the maxLength attribute, limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
<TextView
android:id="#+id/secondLineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:maxLength="10"
android:ellipsize="end"/>

If your not interested in xml solutions maybe you can do this:
String s="Hello world";
Textview someTextView;
someTextView.setText(getSafeSubstring(s, 5));
//the text of someTextView will be Hello
...
public String getSafeSubstring(String s, int maxLength){
if(!TextUtils.isEmpty(s)){
if(s.length() >= maxLength){
return s.substring(0, maxLength);
}
}
return s;
}

Use below code in TextView
android:maxLength="65"
Enjoy...

I did this using the maxEms attribute.
<TextView
android:ellipsize="end"
android:maxEms="10"/>

I am sharing an example where I have set maxLength=1 i.e. limit it to a single line with maxLines attribute, then use the ellipsize=end to add a "..." automatically to the end of any line that has been cut-off.
Please Note: layout_width which is 120dp i.e. after 120dp any text
exceeding will triggrer "ellipsize=end" property
paste the below code directly to check.
<TextView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:maxLength="40"
android:text="Can I limit TextView's number of characters?"
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
.

You can use setEllipsize method of TextView class
http://developer.android.com/reference/android/widget/TextView.html#setEllipsize(android.text.TextUtils.TruncateAt)
With the constants of TextUtil class for added the suspension points
http://developer.android.com/reference/android/text/TextUtils.TruncateAt.html

Programmatic Kotlin.
Cut off the start of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.takeLast(maxChars)
}
Cut off the end of the text:
val maxChars = 10000
if (helloWorldTextView.text.length > maxChars) {
helloWorldTextView.text = helloWorldTextView.text.take(maxChars)
}

Here is the correct Solution. Keep remember, to set width to WRAP CONTENT. and ems is not based on Max Charater. EMS based on width. If ems value width will exceed the text length, then automatically (...) will be added at last of textview.
android:ems="8"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This is a demo text"

As mentioned in https://stackoverflow.com/a/6165470/1818089 & https://stackoverflow.com/a/6239007/1818089, using
android:minEms="2"
should be enough to achieve the goal stated above.

you can extend the TextView class and overwrite the setText() function.
In this function you check for text length or word cound.

Related

How to limit lines on a TextView

I'm trying to limit a TextView to 1 line, the length of the screen. I'm having trouble keeping it from expanding.
I've tried adding the
android:minLines="1"
android:maxLines="1"
attributes to the TextView xml. This does keep the view from expanding, but this does not limit the characters of the text. For example, if I have entered the maximum number of characters in a line and proceed to add a '(' to the string, I can see the top of it printed below the first line.
Basically, I need a way to limit the amount of characters to the width of the screen. I've tried this:
while(txt.getLineCount() > 1) {
txt.setText(txt.getText().subSequence(0,txt.getText().length()-1));
}
which doesn't work at all. I've also tried that same method with comparing the txt.getHeight() to the txt.getTextSize().
I'm out of ideas. What can I do?
If I use a textview this way it will ocupy only 1 line:
<TextView
android:id="#+id/textView"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Try this. This will limit textview to single line. , if the text was more than one line it will display ... at end.
<TextView
android:id="#+id/textView"
android:singleline="true"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Set different text size in Android TextView

I have one TextView with only 1 line of text. Now I have to put an extra line to set the state of the user. So I set the property android:lines="2"
the textview looks like:
<TextView
android:id="#+id/providerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:ellipsize="marquee"
android:fontFamily="sans-serif-light"
android:lines="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
The way I add the text is like:
mNameAndStatus.setText(providerNameText + "\n" + mUserStatus);
Image how is actually.
The problem is that the first lines must me in Large text, but the second line must be medium or small and I don't know if is it possible.
How can I achieve this?
try
String asHtml = firstLine+"<br/><small>"+secondLine+"</small>";
textView.setText(Html.fromHtml(asHtml));
or simply use Spannable (AbsoluteSizeSpan)
You need to do something like this :
mNameAndStatus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
where your text size is given in sp (24sp in the example).
Here are all TypedValues http://developer.android.com/reference/android/util/TypedValue.html

What does ellipsize mean in android?

I've added an EditText to my layout, and added a hint, and made it centered horizontally.
When running the application, the hint was invisible. I found that I should make ellipsize value of the TextView to be start:
<EditText
android:id="#+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="start"
android:ems="10"
android:gravity="center_horizontal"
android:hint="#string/hint1" />
In Android documentation, I read:
If set, causes words that are longer than the view is wide to be
ellipsized instead of broken in the middle.
The problem is that ellipsize is not found in the dictionary. Can anybody explain to me what benefits we can gain by ellipsize attribute? And what is the difference between start, end, middle?
You can find documentation here.
Based on your requirement you can try according option.
to ellipsize, a neologism, means to shorten text using an ellipsis, i.e. three dots ... or more commonly ligature …, to stand in for the omitted bits.
Say original value pf text view is aaabbbccc and its fitting inside the view
start's output will be : ...bccc
end's output will be : aaab...
middle's output will be : aa...cc
marquee's output will be : aaabbbccc auto sliding from right to left
In my experience, Ellipsis works only if the below two attributes are set.
android:ellipsize="end"
android:singleLine="true"
for the width of TextView, wrap_content or match_parent should both be good.
android:ellipsize added in API Level 1. An ellipsis is three periods in a row. (...) .
In your .xml
<TextView
....
android:text="Hi I am Amiyo, you can see how to ellipse works."
android:ellipsize = "end" />
At this point, the ellipsis will not display yet as a TextView is set to automatically expand on default when new text is entered. You will need to limit TextView in some way. Do this, you can use either add to your TextView a scrollHorizontally, minLines, or maxLines to have the ellipsis display.
To make the ellipse:
at the end: this is how it would.
use: android:ellipsize = "end"
And
in the middle:
use: android:ellipsize = "middle"
And
at the start:
use: android:ellipsize = "start"
And
to have no ellipse
use: android:ellipsize = "none"
Note Please :
Do not use android:singeLine = "true", it is deprecated.
android:maxLines = "1" will not display the three dots (...)
android:lines = "1" will not display the three dots (...)
For more details you can visit here
An ellipsis is three periods in a row...
The TextView will use an ellipsis when it cannot expand to show all of its text. The attribute ellipsized sets the position of the three dots if it is necessary.
Text:
This is my first android application and
I am trying to make a funny game,
It seems android is really very easy to play.
Suppose above is your text and if you are using ellipsize's start attribute it will seen like this
This is my first android application and
...t seems android is really very easy to play.
with end attribute
This is my first android application and
I am trying to make a funny game,...
Here is an example on how ellipsize works without using deprecated android:singleLine="true" in a ConstraintLayout:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="13sp"
android:ellipsize="end"
android:maxLines="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="long long long long long long text text text" />
remember if you have a text that is supposed to be in a single line, then change the maxLines to 1.
Note: your text must be larger than the container box for the following to marquee:
android:ellipsize="marquee"
Set this property to EditText. Ellipsize is working with disable EditText
android:lines="1"
android:scrollHorizontally="true"
android:ellipsize="end"
android:singleLine="true"
android:editable="false"
or setKeyListener(null);
Use ellipsize when you have fixed width, then it will automatically truncate the text & show the ellipsis at end,
it Won't work if you set layout_width as wrap_content & match_parent.
android:width="40dp"
android:ellipsize="end"
android:singleLine="true"
As it is already mentioned above if there is no enough space for text (due to the size of the layout or set limitation) ellpsize will add ... dots to it.
Here an example:
Top: with ellipsize (here it is set to 'end'
Bottom: without

How to set text length in button

I want to have a fixed size button
If the text is longer than button width , it should show "tex..."
How do I do this?
Ellipsize can be used but there is a bug. The workaround is to set scrollHorizontally to true and lines to 1. See the following code example:
<Button android:text="Button"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:scrollHorizontally="true"
android:lines="1"
android:ellipsize="end">
</Button>
Please try below code in button
android:maxLength="5"
I'd use aString.length(); and if it is greater than whatever int you choose, shorten it with aString = aString.substring(0, aString.length() - int); and make that int aString.length() - the max length you want. then take the new string and append the ... with aString.append("...");
Use a textview instead of a button and use the ellipsize property to get the 3 dots. Also you will need to have a click listener on the textview to make it function as a button.
You will also need to use : android:scrollHorizontally="true" for the "..." to appear ( bug in android :-( )

android append '…' at the end textview _EDIT

I have a lsit view and i that i need to add some text.
in the adapter, I need to append ... at the end and i used the following
when i give
android:ellipsize="end"
in the only shows 2 line
eg:
asdnfsdfdsf asdfsdfasdf
sdfsd sdfsdf sdfsd ad...
And when i give
android:ellipsize="start"
it is as :
asdnfsdfdsf asdfsdfasdfd
...sdfsd sdfsdf sdfsd ad
The complete code i used:
<TextView android:layout_width="wrap_content" android:background="#016A7D"
android:layout_height="80dp" android:textColor="#000000"
android:maxLines="4"
android:id="#+id/list_report_desciption"
android:ellipsize="end"
/>
And the output i got is:
What exactly will a smarter planet
look like? How's IT changing? A...
actually it contains more than 6 lines
Is there any thing i need to set so that i need to get 3 lines
Thanks
Text ellipsization seems buggy, accroding to this report:
http://code.google.com/p/android/issues/detail?id=10554
A workaround allows ellipsizing one line of text, by setting the android:singleLine attribute to true. But this attribute is deprecated.
Nothing worked for me:-(
I just fixed the length. Cut the string in that length and append '...' after that.
Now its working for me.
I was looking for a simple solution for this myself, I ended up with something like:
private String getTextWithMoreIndicator(TextView textView, String text) {
long maxVisibleChars = textView.getPaint().breakText(text, true, textView.getMeasuredWidth(), null);
if(maxVisibleChars < text.length()) {
text = text.substring(0, (int)maxVisibleChars -3) + "...";
}
return text;
}
The solution you accepted will not scale correctly on all devices.
Reason : the width of "a" is lesser than "A", so, if you are truncating string based on some size (say 15 letters) and adding "..." through code. You might see results not expected.
Now coming to the solution:-
Solutions:
Solution # 1 :
Add the following 3 attributes to your TextView and you'll get the result you want :)
<TextView
android:ellipsize="end"
android:lines="1"
android:scrollHorizontally="true" />
Solution # 2 :
Another workaround might be, you could decide to marquee text (the fancy animation moving your text from right to left). For that you need following attributes in your TextView xml:-
<TextView
android:id="#+id/my_text_view"
android:ellipsize="marquee"
android:lines="1"
android:scrollHorizontally="true"
android:marqueeRepeatLimit="marquee_forever" />
And then in you code you need to get the TextView by its id and put-in the following line:-
TextView myTextView = (TextView) findViewById(R.id.my_text_view);
myTextView.setSelected(true);
#### Edit:- ####
I just figured out that for my solution to work in android versions greater than 2.3.x we need to add the following line in our TextView xml :-
android:singleLine="true"
Although its a deprecated attribute, but you have to add this, otherwise marquee or "..." wont work.
Hope this answers the question :)

Categories

Resources