Keeping Textview content on 1 line and prevent wrapping - android

I would like to have a text displayed on an Android widget (remote views) not wrap over its text to the next line, if the text is longer than the Textview's width. Instead of that, I would like to have the Textview cut any extra text. I have tried many things without success.
Here is the XML code for the TextView:
<TextView
android:id="#+id/txtInformation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:maxWidth="220dp"
android:lines="1"
android:text="Long text long text long text long text"
android:textSize="13sp" />

In your layout XML, use android:maxLines="1" or android:singleLine="true"

Related

How to set text leading or shift a text position on a TextView

How do you set a text leading on a TextView? Or how do you shift a text position relative to a view that it is in?
I want the height of the textview to be less than what wrap_content would set at default without cutting the text from the bottom.
I've tried using the layout_height tag on its xml layout but it would clip the text from the bottom just like the image I've provided on the link below, and I also have tried using the lineSpacingExtra tag but it seems to only work for the distance/leading between two lines.
Here is my xml file:
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:text="clipped text"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lineSpacingExtra="-18dp"
android:text="a text longer than usual to demonstrate line spacing"/>
Image for clarity: https://i.stack.imgur.com/QGV4B.png
Why you can use wrap_content? Or can you share what are you trying to achieve?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clipped text"/>
Did you try to do it using padding or paddingTop ? Try it with different values to get your desired result.
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:text="clipped text"/>

How to make TextView looks exactly like EditText?

How to make the whole TextView underline at the width of match parent like EditText?
This line of code only underlines the text, not the whole TextView.
textView.setPaintFlags(textView.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
The reason I want to use TextView is that setOnClickListener is triggered immediately in one tap for TextView, whereas two taps are required for a disabled editable EditText.
You can put the background of an EditText on your TextView. For example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text"
android:hint="My hint"
android:textColor="?attr/editTextColor"
android:background="?attr/editTextBackground"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
/>
You might want to change the text appearance to android:textAppearance="#style/TextAppearance.AppCompat.Button" if you are using an AppCompat theme.
You can achieve this without writing many lines of xml attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text"
android:hint="Your hint"
style="#android:style/Widget.EditText"
/>

Android TextView SingleLine field hides long text

I have a TextView which sits on the left side of the screen and is set with gravity="right" and it is set with SingleLine = "true."
If by some chance the text in this view gets too long I want it to simply disappear off the left hand side of the view. I thought the configuration below would do that but what actually happens is the the long string disappears completely, presumably down and outside of the view somewhere.
How can I create a simple text view that contains a single line of text and keeps its layout even when something unexpected happens? ... or even something predictable.
<TextView
android:id="#+id/tempF"
android:text="#string/tempF"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:textStyle="bold"
android:textSize="92dp"
android:fontFamily="serif"
android:gravity="right"
android:layout_marginTop="60dp"
android:layout_gravity="top|left"
android:singleLine="true"
/>
This is the purpose of "ellipsize" - truncating a portion of text to indicate additional text.
In you case, you may simply need to add something like:
android:ellipsize="end"
or you might need to go deeper:
Automatically ellipsize a string in Java
Or look at this:
android ellipsize multiline textview
The idea behind extending the class is that you can customize the behavior of the TextView when the text content exceeds the space provided. For example, you can give it the appearance the it "bleeds over" by removing padding, etc. An ellipsis is an indicator that is commonly used to explain "there's more text that you can't see" - this is how it would look:
This is really a really long...
(the 3 periods are the ellipsis - your text goes the opposite direction, but it should still work)
With a custom TextView, you could change the "..." to nothing or anything else you want (even an image, which I've done).
You could also marquee the text:
TextView Marquee not working
Add 2 properties in xml
android:singleLine="true"
android:ellipsize="end"
You should play with ellipsize attribute of the TextView.Check below:
<TextView
android:id="#+id/tempF"
android:text="#string/tempF"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:textStyle="bold"
android:textSize="92dp"
android:fontFamily="serif"
android:gravity="right"
android:layout_marginTop="60dp"
android:layout_gravity="top|left"
android:singleLine="true"
android:ellipsize="end"
/>
<TextView
android:id="#+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorGray"
android:textSize="#dimen/_18dp"
android:padding="#dimen/_2dp"
android:singleLine="true"
android:ellipsize="end"
android:text="#string/desc" />
Since android:singleLine is deprecated. We can use this line android:maxLines
Can do like this.
android:ellipsize="end"
android:maxLines="1"
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
It works.

TextView's ellipsize not working on maxLines = 1

I really cannot figure out why, but I am only able to get ellipsize working on maxLines=2 and more. I am displaying a few words of description and then a long string with no spaces.
This is how the TextView looks like:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#757575"
android:text="#string/gcm_not_registered"
android:maxLines="1"
android:ellipsize="end"
android:id="#+id/login_gcmRegistrationTextView"/>
I then programatically set a text to it, but depending on the maxLines limitation, I get two different results:
The only thing that changed was the maxLines, why isn't the line filled in the first picture as well?
There are two ways to fix it:
Try to change android:ellipsize="end" attribute to android:ellipsize="marquee".
Try to remove android:maxLines="1" android:ellipsize="end" attributes and add android:singleLine="true" attribute.
This code works for me:
In the xml add:
Attribute ellipsize: marquee
Attribute lines: 1
In java:
<yourTextView>.setHorizontallyScrolling(true);
<yourTextView>.setSelected(true);
If there is another item that request the "focus" you lose the marquee effect. The textView need the state selected to prevent this.
Actually the problem is with the spannable text, if you set spannable text this wouldn't work. Other than this below code works for me
<TextView
android:id="#+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_margin="5dp"
android:padding="5dp"
android:fontFamily="sans-serif-condensed"
android:textColor="#b3277b"
android:background="#f7ecff"
android:layout_below="#id/tv"
android:text="This is first line\nThis is second line\nThis is third line"
android:ellipsize="end"
android:maxLines="2"
/>

Android: Text in TextView not aligned well

I have a TextView in RelativeLayout in which I am setting the text from a string and that string contains the text more than 2 lines. But I want to set the Text into two lines. All is working but the problem is the Text in two lines is not aligned well.
Here is how it looks:
This is the text of first line
which contains a dummy text
As you can see I have set left margin of 5 then it only applied on first line but not on the second one. So what is the problem and how can I solve it?
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/image"
android:maxLines="2"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceMedium">
</TextView>

Categories

Resources