Getting TextView to wrap - android

I know this question has been asked a number of times but I still can't seem to get this to work.
I have tried all sorts of combinations and it just refuses to wrap to 2 lines.
This is what it is now:
<TextView
android:id="#+id/doctor_details_clinic_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="3dip"
android:textSize="12sp"
android:maxLines="1"
android:layout_gravity="right"
android:gravity="right"
android:scrollHorizontally="false"
android:singleLine="false"
android:ellipsize="none"
/>
I've tried make the width 0dip and all other things suggested but to no accord.

First of all you should set Max lines to 2 instead of 1.
Something like this will work:
<TextView
android:id="#+id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:text="this is the most interesting use of text view I have seen so far,this is the most interesting use of text view I have ever seen so far,this is the most interesting use of text view I have ever seen so far this is the most interesting use of text view I have ever seen so far"/>

Related

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.

force android TextView to wrap

I am using GridLayout from the support Library. Some of the cells contain player information - I use a Linear Layout with two TextViews. I have not been able to get the name (the second text view) to wrap.
Current behavior:
123
John Cochtousczyon
Desired behavior:
123
John
Cochtousczyon
Here's the current state of the cell layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/player_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="test" />
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:minLines="2"
android:scrollHorizontally="false"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
</LinearLayout>
and the code that adds him to the grid:
lp = (GridLayout.LayoutParams) playerCell.getLayoutParams();
lp.columnSpec = GridLayout.spec(nameCol);
lp.rowSpec = GridLayout.spec(nameRow);
playerGrid.addView(playerCell, lp);
Ideally I would like a solution that sets the column width for the widest cell AFTER wrapping the name. I've gotten close to the desired result using maxEms, but that requires balancing the most likely needed limit against how much truncation to tolerate with longer names.
P'r'aps there's nothing for it but to fix the width - just thought I'd ask in case all the smarter people than me out there have solved this.
Here's what I've settled on for now, until something better comes along:
<TextView
android:id="#+id/player_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="4"
android:maxLines="2"
android:text="testing thisout with extra sauce"
android:textSize="18sp" />
combination of fill_parent, ellipsize END, maxEms and maxLines gets me close enough.
If anyone is still looking this in 2020 Android now has a inbuilt way of doing this. Just add this code to your java file
TextviewCompat.setAutoSizeTypeWithDefaults(<your textview refernce here>, TextviewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
Also make sure you add this to your textview xml
android:ellipsize="none"
Hopefully this will help someone;
I simply used the good old "\n" to wrap.

TextView Marquee not showing '..'

I already tryed alot of solutions, maxLine, maxLenght, marqueeAlwaysShowing, the ellipsize with marquee..the focusable thing.. I can't can't just get it working.
I erased all the additional XML to make it working and here it is, this is what I have:
<LinearLayout
android:id="#+id/promptDefine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/counterDefinitionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:onClick="nameCounting"
android:text="#string/nameIt"
android:textColor="#color/count_item_to_be_defined"
/>
</LinearLayout>
So, what should I have to add it at textview to limit text to 10 characters ? but if they are more just insert the "..." at the end. I already tryed some things like:
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
And didn't work, thanks in advance!
EDIT:
Maybe its the Clickable feature attribute that is messing this ?
The three dot only shows when the textView doesn't have enough space for the characters.
in your case just set a fix width and set single line. then it should work.
<TextView android:id="#+id/counterDefinitionName"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:onClick="nameCounting"
android:text="#string/nameIt"
android:singleLine="true"
android:textColor="#color/count_item_to_be_defined" />
not "clickable", make it "selected"

Multiline TextView with width "wrap_content"

I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Any thought welcome.
EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines
Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Long multiline text"/>
<TextView
android:textColor="#color/text_color"
android:layout_width="130dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
Also add
android:minLines="2"
android:scrollHorizontally="false"
You could try
android:inputType="textMultiLine"
in your TextView XML. This worked for me.
I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.
I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.

TextView doesn't get truncated in TableLayout although using ellipsize attribute

I've got some problems with a table layout I used to show communication data within an app. The problem is that the value of the communication data may be longer than the available width so the text got cut off what doesn't look very nice.
To solve this issue I googled a little bit and found several solutions that advise to use the shrinkColumns attribute on the TableLayout and the android:ellipsize on the TextView that should get truncated.
This is the xml for the table
...
<TableLayout android:id="#+id/table"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:shrinkColumns="1"/>
...
The rows of the table get inflated on runtime using following row layout.
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView android:id="#+id/type_column_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:ellipsize="end"/>
<TextView android:id="#+id/value_column_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:ellipsize="end"
android:autoLink="all" />
</TableRow>
I don't know what I'm doing wrong but this drives me insane. Any suggestion how to solve this or how to find the reason why it doesn't work?
Remove the line that says:
android:inputType="text"
Then you should get ellipses.

Categories

Resources