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.
Related
I have a TextView with a height depending on previous content, but there might be a long Text in it. How can I cut it off at the right point and concatenate three dots or something similar to the new end?
Currently it looks like this:
I found some solutions for Single Line Text, but how does it work with more than one line? I also do not know the number of lines, because this depends on the screen size.
Are there other typical ways on Android to show that the text can be extended? E.g. a colour gradient in the last line?
Edit:
When I do it without a fixed heigth, I have to make the height depend on the element above and my XML will look like this:
<TextView
android:id="#+id/podcastShortDesc"
android:text="Long text"
android:layout_below="#+id/podcastTitle"
android:layout_toRightOf="#+id/podcastLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:ellipsize="end"
android:layout_above="#+id/podcastMoreAction" />
When I do specify maxLines I can have luck an it will work:
But if the title is too big, it does not work:
You should add following code for "3 dots" at the end.
android:ellipsize="end"
You should remove fixed height property
android:layout_height="50dip"
instead you should add number of lines
android:maxLines="4"
android:layout_height="wrap_content"
Android will take care everything else. In this way, even if text is smaller than 4 lines, android will take care size. If it is more than 4 lines, it will add "3 dots" :) Specify fixed height may cut your text.
Try this. Hope it will work.
<TextView
android:id="#+id/podcastShortDesc"
android:text="LONG LONG LONG TEXT"
android:layout_width="match_parent"
android:layout_height="50dip"
android:maxHeight="50dp"
android:ellipsize="end"/>
I have tested 50dp can show two line in normal font size. So in 50dp height you should add maxLines 2.
Try this fix layout_height and add scroll in your textview
<TextView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="50dip"
android:text="Hello......"
android:scrollbars="vertical"/>
I'm working on a layout that includes both English and Hebrew (intended as right-to-left) text, in separate views. When a line of Hebrew text gets beyond a certain length, it becomes written left-to-right (I assume that this has to do with length because it looks fine with shorter text, and when I display it on a tablet instead of a phone).
The relevant view looks like this:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:gravity="center"
android:textDirection="rtl"
android:text="#string/tx_middle_text"
android:textSize="#dimen/big_text_dp" />
and the string is defined in strings.xml like this:
<string name="tx_middle_text">טקסט בעברית</string>
(I've replaced the original text, which was 50 characters long, and made up entirely of Hebrew letters and white-spaces).
Note the text has the rtl attribute. I've tried replacing it with anyRtl, and I've tried changing gravity to "right" - neither helps.
I need the text to remain in one line and be cut off with an ellipsis if it doesn't fit - as it is, that's what happens, but with the text written left-to-right.
How can I fix this?
Edit: For an ad-hoc solution I made a shorter string as an alternative resource for the smaller layout (it works as long as the text is less than one line long on a given device), but I'd still like to know if there's a general solution to this.
RTL is detected automatically by the system depending on the characters. i.e for Arabic characters, text will be drawn from right to left.
This is an example on how my spinner is drawn. For this TextView:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_item"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="15sp"
android:gravity="center_vertical|start"
android:textColor="#color/spinner_filter_text"
android:ellipsize="end"
android:singleLine="true"
android:paddingStart="20dp"
android:paddingEnd="20dp"/>
And these strings:
<string name="all_categories">All Categories</string> (values)
(values-ar)
The result is:
If I modify the TextView with:
android:layout_width="100dp"
The result is:
Which I think is fine... Note that in strings.xml the string direction for ar language is wrong but Android is drawing it properly. I guess it is because of some Android Studio setting.
Replace android:singleLine="true" with
android:lines="1"
android:maxLines="1"
also add android:ellipsize="end"
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.
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...
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 :)