android append '…' at the end textview _EDIT - android

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 :)

Related

setHyphenation and breakStrategy don't work as expected in TextView

I was trying to adding hyphenation on breaking long words in TextView. For example, I have a textView which text is "Recommendations". Because the word is too long for one line, I want to get "Recommend-" in the first line and "ation" in the second line like this :
But In fact, I got
Here's what I have for the TextView :
<TextView
android:id="#+id/textViewid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/long_text"
android:breakStrategy="high_quality"
android:hyphenationFrequency="full" />
I have tried different combination of breakStrategy value and hyphenationFrequency value, but didn't make it work.
Any idea of why it behaves this or other ways to make hyphenation working?
Thanks in advance!

Limiting a String to 2 lines in android, then cut it and concatenate a String

My question may sound a bit weird but I'll try to explain it better here.
I ahve a TextView in android, at the inferior part of my activity. I want to have it limited to 2 lines, which is easily reachable by adding the following line in the TextView xml element:
android:maxLines="2"
Okay, now we've got it limited to 2 lines.
Then, in my Activity, I make:
termsandconditions = (TextView) findViewById(R.id.textView1);
termsandconditions.setText(terms);
Okay, now I've got a big string with the terms and conditions, but limited to 2 lines due to the xml attribute.
Now my question is, how can I cut it after having it limited to 2 lines, and concatenate a string with "Read more"? I don't need it to be in the same textView or whatever, I only want that it looks like:
Terms: blablablalblalbla blal blablalblalblalblalbla lalblalblalblalblalblalb lalblalblalb lalblalblalblalblalb lalblalblalb lalblalblalblalb lalb bla View More.
Thanks and I hope you can understand my problem.
You can use setEllipsize (TextUtils.TruncateAt where) method of TextView or the android:ellipsize XML attribute.
public void setEllipsize (TextUtils.TruncateAt where) Added in API
level 1
Causes words in the text that are longer than the view is wide to be
ellipsized instead of broken in the middle. You may also want to
setSingleLine() or setHorizontallyScrolling(boolean) to constrain the
text to a single line. Use null to turn off ellipsizing. If
setMaxLines(int) has been used to set two or more lines, END and
MARQUEE* are only supported (other ellipsizing types will not do
anything).
Related XML Attributes
android:ellipsize
Might be a bit hacky, but here's my suggestion:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="your_text_view"
android:text="long long long text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="link"
android:text="View More"
android:onClick="addMoreLinesAndHideThisTextView"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Some additional tweaking may be needed, but I think you got the idea.

Android TextView with multiple lines

I want a TextView that should be broken into 4 lines. For e.g.
Vishal Vyas
Having
342
Reputation
Note, the gravity should be center_horizontal
I tried following :
<TextView
android:gravity="center_horizontal"
android:id="#+id/lblUserRep"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:text="VishalVyas Having 342 Reputation" >
</TextView>
This works! but produces following output:
VishalVyas
Having
342
Reputation
Problems:
It doesn't work with the space between words Vishal and Vyas.
android:layout_width="70dp" is harcoded and there can be any name with n number of characters instead of VishalVyas.
Please advice.
Added:
It would be fine if I need to write a custom TextView for achieving this but I'll require some guidance.
Thanks in advance.
I think it's wrapping because "Vishal Vyas" is going beyond 70dp. Instead, do wrap_content on the width and use newline characters for lines instead of wrapping (i.e. "Vishal Vyas\n342\nReputation")
You should be able to insert the newline character \n to control where the splits go. Once you do that, you can expand your TextView wider so that it can accommodate a longer user name but still break in the right place.
android:lines="2"
android:minLines="2"
android:singleLine="false"
Even if Android Studio warns that android:singleLine=false is deprecated, keep it and one can have the number of lines they want for their text box depending on the length of their text
I did so:
The Container of TextView:
android:layout_width="match_parent"
android:layout_height="match_parent"
The TextView:
android:layout_width="match_parent"
android:layout_height="wrap_content"
Then,
The Text of the TextView was showed in two lines or more...

How can I find the number of characters OR string elements per line line in Android

I have a TextView with a very large string that uses multiple lines when compiled. I am trying to figure out how I can find the number of string elements or characters there are in each line.
The XML for the TextView
<TextView
android:id="#+id/textViewSentenceP"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:gravity="center"
android:singleLine="false"
android:padding="25dip"
android:scrollHorizontally="false"
android:textColor="#FFFFFFFF"
android:maxLines = "5"
android:scrollbars = "vertical"
android:textSize="12sp" />
My code works perfectly i just need to know how I can find the number of elements in each line.
Also, if this can not be found then if I can determine which element is at the end the line of text or which element starts the next line of text will also be a fine answer. Thank you!!
Never mind I got it!!
Using the getLineStart method fixed the problem! For anyone who was having a similar problem to me,try this code, maybe it will help you! Note: the getLineStart() will read all the characters from every line including spaces in order till it gets to that specific line.
int curLine = tv.getLayout().getLineStart(0);
int nextLine = tv.getLayout().getLineStart(1);
int difference = nextLine-curLine;
The value of difference will be how many characters (including spaces) are on the curLine

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 :-( )

Categories

Resources