setHyphenation and breakStrategy don't work as expected in TextView - android

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!

Related

Word has not enough space in single line

I have an TextView with a big fontsize and if I have a long word only the last char is put into the next line. For example :
Zusammenarbei
t
Now I would like to format the text to look like this:
Zusammenarb -
t
Is there a possibility to achive this?
I guess better than doing Hyphenation, you can do something better.Consider the following image.
I guess green one will be much suitable for your need. Just declare your textview as follows:
<TextView
android:id="#+id/yourUniqueID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:ellipsize="end" <!--THIS WILL DO FOR YOU-->
android:maxLines="1"
android:text="test"/>
Look at the break strategy of the TextView
android:breakStrategy="high_quality"
constant value description
high_quality 1 Line breaking uses high-quality strategy, including hyphenation.

RoboTextView ellipsize adds more text after the "..."

I use the following RoboTextView xml declaration:
<com.package.utils.RobotoTextView
android:id="#+id/ttli_small_desc"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="6dp"
android:textSize="12dp"
android:paddingBottom="6dp"
android:maxLines="5"
android:ellipsize="end"
android:textColor="#color/selectable_text_black_to_white"
bit:fontType="light"/>
It is a custom view object, but I've tested it with a standard TextView and the same problem occurs. The only thing this custom view does is allow me to use RobotoLight font. A bit outdated of a technique now but it's there so I use it.
Anyways what I'd expect is a maximum of 5 lines which should look like this
word words words
words words words
word word words
words word
words words words...
But here is what I'm getting:
Basically it ellipsizes the text, but then just picks right back up afterwards. I've played around with singleLine=false and other random attributes trying to fix it but I've struck out so far. Does anyone know how to fix this?
Thanks!

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...

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