How to truncate TextView, and then add ellipsis in Android - android

I have read a few of the other threads here with similar concerns, but none of their answers seemed to work for me.
Can't get ellipsis to work on Android
Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?
I have a Relative Layout with 2 elements, a text view and an image view. I need the text view to be 1 line and to truncate about 5sp short of the image view and apply ellipsis to the end of the text.
<RelativeLayout
android:id="#+id/title_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp">
<TextView
android:id="#+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="10sp"
android:textColor="#FFFFFF"
android:text="title"
android:maxLines="1"
android:ellipsize="end"/>
<ImageView
android:id="#+id/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5sp"
android:layout_marginLeft="5sp"
android:background="#drawable/home"/>
</RelativeLayout>
In the above code, I was attempting to apply a margin left to the Image View to force the text view to its left to truncate by as many sp as I put in the margin.
The code above would truncate the text view to a single line and add (...) but only to the end of that line; irrespective of the ImageView to it's right, regardless of whatever margin I apply to it. The Image View appears above the text. I figured this may be due to them being in a relative layout.
Any help in figuring out how to make the text view respect the margin/space taken up by the image view would be greatly appreciated.

I believe what you want to do is to first place the image view (with margins) and then set the text to align to the left border and to be placed to the leftOf the image.

Related

Max lines with height set as wrap_content

I have two TextViews in a vertical LinearLayout, one serves as a display for a book's title and the latter as a display for the book's author(s).
I need the first to have wrap_content as its height, so it takes a good part of the linear layout. However, I want it to cap out at three lines max, so that there is still some space left for the second text view;
and I need the latter to fill the remaining space (0dp and layout_weight=0dp).
I want to use specific configuration so that the author view will be always right after the title view (on its bottom).
Something like this, however the max_lines do not kick in.
I tried to set max_lines to 3 and wrap_content for the first view height, but it seem that max_lines is ignored if height is set to wrap_content.
I also tried to circumvent the problem by sort of cheating and adding a max_height, but then the two views may be spaced apart from one another.
At last I tried to convert the linear layout to a constraint layout, to see if I could access some other layout settings to no avail.
Any help?
It seems to be working fine on my side with android:max_lines="3" even with wrap_content
This is just a test layout I created
<TextView
android:id="#+id/textview1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:maxLines="3"
android:textSize="#dimen/_13sdp"
android:textStyle="bold"
android:text="Very Very Very Very Very Very Very Very Very Very Very Very Long Text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Very Very Short Text "
app:layout_constraintStart_toStartOf="#+id/textview1"
app:layout_constraintTop_toBottomOf="#+id/textview1" />
Screenshot ->
Hope this helps! :)
Replacing app:max_lines with android:max_lines seems to have fixed the issue.

Android: Cut off to long multiline text in TextView

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

Display view to right of text

I want to display text (track title) and a view to right of the text. Like on the image below.
The view (image on the screenshot above) should always be visible. The view should be displayed right after the text if there is more room than necessary. The text should be truncated if there is not enough room.
The issue is: if the text is long enough the view is not displayed or displayed in smaller size.
Please don't recommend me to use text view with a drawableEnd attribute because the view not always will be an image view.
Here is the layout I use:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/trackTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:text="#string/placeholderTrackTitle"
android:textSize="#dimen/track_name_height"
android:textColor="#color/queue_text_color"
android:layout_weight="0"/>
<ImageView
android:id="#+id/downloadedIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_downloaded"
android:layout_gravity="center_vertical"
android:layout_marginLeft="#dimen/queue_horizontal_margin"
android:layout_marginRight="#dimen/queue_horizontal_margin"
android:layout_weight="1"
android:contentDescription="#string/downloaded_icon_description"/>
</LinearLayout>
Use the maxWidth attribute on the TextView and ellipsize the text in case of overflow.
edit: or MaxLength, MaxLines, or MaxEms, whichever works better for your purposes
ok, there are few solutions that i have used and it works good.
Check the below one:
1) Android: Something better than android:ellipsize=“end” to add “…” to truncated long Strings?
2) How to truncate TextView, and then add ellipsis in Android

android textview text direction now responding to changes

I am trying to do the right to left text direction, but I am getting unexpected result (not like the one when I do html css). Here is the code I have for that textview:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="96dp"
android:gravity="right"
android:text="#string/welcomeMsg"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white"
android:textDirection="rtl"
android:textSize="#dimen/welcomeMsgSize" />
I believe I did everything there is to do. Maybe I forgot something or don't know it. I am pretty new to android. The picture shows the output of the above The problem here is the whitespace preceding the text inside the textview and post the text too. Thanks for your help in advance :)
You should change Textview gravity from "Center" to "Right" or "Left" Depending on your need.
Gravity will set gravity to your text so change it. And also change the text alignment from center to right or left.
android:textSize="#dimen/welcomeMsgSize" there lies the culprit.. reduce the size you specified in that file there..!!

Off-center alignment of text inside a TextView

I have a layout/alignment problem with a TextView which I haven't yet been able to find a solution for, that is, I want to align text off-center inside a TextView horizontally.
To give a little more context, I have an ImageView and a TextView side by side, the ImageView touching the left edge of the screen and the TextView filling the rest of the screen horizontally, like this:
[-img-|-----text-----]
The TextView is configured as singleLine="true" and maxLines="1", so that it will be truncated if its too long for its horizontal space. My aim is to align the text in the center of the screen, not the center of the TextView, because there are other elements on the screen that are aligned to the center and I need the text aligment to match that.
So, if I use gravity="center" on the TextView, I get the image above, but what I actually want is
[-img-|--text--------]
I tried putting image and text in a RelativeLayout, so that the TextView actually touches both edges of the screen, which does what I want with respect to the alignment, except that if the text is long enough, the first characters will be hidden by the image, since the TextView lies behind the image view.
I also experimented with margins, padding and a compound drawable to the left, but the text is always centered relative to the available space (which I would consider the expected behavior).
Does anybody have any clues on how to achieve this alignment, i.e. relative to the center of a different component than the TextView, maybe programmatically at runtime? Thanks in advance for any helpful advice.
Edit: user Budius suggested using padding to the right to achieve a centered alignment which works but leads to long texts being truncated before the right edge of the TextView is reached and I'm looking for a solution that avoids that, i.e. that uses the entire avilable space, if possible.
I believe that will center it and the move 20dp to the side.
gravity:centre padding:right=20dp
Try using
android:layout_weight="1"
if it helps you..
I know this is not the correct approach than too give it a try..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight=".33"
android:gravity="center"
android:text="text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".33"
android:src="#drawable/ic_launcher"
android:visibility="invisible" />

Categories

Resources