I have an issue where my TextView has characters clipped on its right side only for certain pieces of text. To specify: I don't have a custom font or italic text; it's standard text. Here's an image, there should be an "i" where I have the circle, it's "nitrosoethane" not "ntrosoethane":
I have found multiple workarounds to fix the clipping, such as setting a text shadow or sub-classing TextView. They work, but they don't fix one particular issue. I have a SharedElementTransition with this TextView, and only when returning to the calling Activity, as part of the SharedElementReturnTransition animation, the TextView decides to wrap on a different character. Interestingly, this wrap is in the correct place to avoid any clipped text. Unfortunately, it ruins the animation effect, because the text jumps to its bugged-out position at the end. Here's a video, as you can see, the text above the line reads "nitrosoethane" for a split second before it switches to "trosoethane":
I have done a lot of investigation, but I'm really scratching my head on this one. I can neither fix the TextView so it wraps on the correct character or make the TextView in the animation portray the bug for animation continuity. I've measured the TextView before, during, and after the animation, and it's always the same width with the same text and the same font and the same size — same everything (except measuredWidth is larger on the SharedElementEnterTransition, but that shouldn't effect this, and regular width is the same anyway). I don't see why the word wrapping would change only on the return transition. Please help.
Here's the XML for the calling Activity's TextView:
<com.google.android.material.textview.MaterialTextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:elevation="4dp"
android:ellipsize="end"
android:textColor="#color/text_primary"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#+id/image"
app:layout_constraintStart_toStartOf="#+id/card"
app:layout_constraintTop_toBottomOf="#+id/formula"
tools:text="Magnesium Chloride" />
And here's the opening Activity:
<com.google.android.material.textview.MaterialTextView
android:id="#+id/anim_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="8dp"
android:elevation="4dp"
android:ellipsize="end"
android:textColor="#color/text_primary"
android:textSize="18sp"
android:transitionName="#string/history_anim_name_tran"
app:layout_constraintEnd_toStartOf="#+id/dummy_thumb"
app:layout_constraintStart_toStartOf="#id/background"
app:layout_constraintTop_toTopOf="#+id/name"
tools:text="Magnesium Chloride" />
This issue doesn't happen for most text, just text like the one I've shown. Also, if you're wondering, I set the transitionName programmatically for the first TextView. In addition, these two TextViews are in RecyclerViews.
I believe I found the issue. It has to do with my TextView's width being dependent on the ImageView's width (which is not know on the first layout pass). For some reason, the TextView correctly changes its bounds after the ImageView width is updated when the image loads, but the TextView still sometimes incorrectly draws some text outside its bounds (which is usually clipped, but can be seen in my question due to a workaround). Calling invalidate() or requestLayout() doesn't do anything to fix it. But I found a workaround:
If I set the TextView to fixed width, the text fits correctly. So as a workaround, I allow the ConstraintLayout to layout the TextView first, then I manually set the TextView width to itself.
// Set "match constraints" to determine width
textView.layoutParams.width = 0
textView.text = "Some text"
// Make the width a fixed number after OnLayout()
textView.doOnLayout {
textView.layoutParams.width = textView.width
}
This seems like a bug in ConstraintLayout, TextView, or their interaction. This fix works in most all cases, except mine, because the card itself is an itemView in my RecyclerView. I'm not sure why, but the solution doesn't work in my onBindViewHolder(). Instead, I simply set the ImageView to a fixed width and call it a day.
I think an image will explain this far better than words. The screenshot is from the layout viewer thingy in Intellij, but this happens on device as well. I want the cut off text to not be displayed at all. Is this possible?
<TextView
....
android:lines="2"
or:
<TextView
....
android:maxLines="2"
First option sets exact number of lines for your text view, second options sets the maximum number.
I have an oddly organized activity with a number of LinLayouts inside of LinLayouts inside of LinLayouts (inside of LinLayouts) all so that each little section is organized correctly. Everything looks good except for one part.
I have a vertical LinearLayout with two things inside of it. One is another LinearLayout with weight set to 6, and below it is a TextView weight set to 1. What I want to do is have the height of the TextView to scale depending on the amount of lines in it. It could either be a one-liner sentence or maybe a four-line paragraph--I don't know what it'll end up. Then the LinLayout above it needs to fill in the rest of the height.
What layout_height settings do I need to combine to get this to work?
Finally figured it out. It's sort of weird, but this is what I had to do:
<LinearLayout
android:id="#+id/everything_but_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/paragraph" />
I had to have both set to "wrap_content" but give the one I wanted to fill the remaining space a weight WITHOUT giving the other one a weight. Very weird workaround but it works perfectly.
I want two TextViews to overlap such that individual letters will overlap perfectly (i.e. you can't even see there is another TextView on the screen). Here is the relevant part of my layout:
<com.testing.android.animals.OutlinedTextView
android:id="#+id/label_left_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Elephant"
android:layout_alignTop="#id/picture_left"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="40dp"
android:background="#99000000"
></com.testing.android.animals.OutlinedTextView>
<com.testing.android.animals.OutlinedTextView
android:id="#+id/label_left_letter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="E"
android:layout_alignBaseline="#id/label_left_name"
android:layout_alignLeft="#id/label_left_name"
android:textColor="#ffffff"
android:textSize="40dp"
android:background="#99ffffff"
></com.testing.android.animals.OutlinedTextView>
When rendered, it looks like this:
Notice the individual "E" (in light gray) is aligned too high.
What can I change in the layout that will get them to overlap better?
The issue is that the p makes the bottom of the full word textview taller. and since you have wrap_content as the height, the two heights of the textviews are different.
If you set the height of each textview to a specific size (the same size), they should align.
But in general, i think your approach might be wrong - you may want to either look into spannable strings for formatting or, since it appears you are using a custom control anyway, have it do the formatting of the first letter. (here is an example of spannable strings: http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring )
Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.
Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs
For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.
For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.
Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
This should fix your problem: android:layout_weight="1".
By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.
OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.
In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.
Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.
Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.
I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.
All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"
I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"
Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.
Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width
You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>