Home » Android » layout – Android: Last line of textview cut off

layout – Android: Last line of textview cut off

Posted by: admin March 11, 2020 Leave a comment

Questions:

I have a horizontal LinearLayout containing a TextView followed by a Spinner next to it. This LinearLayout is dynamically inflated multiple times in a fixed vertical LinearLayout contained within a RelativeLayout.

The problem is that since I switched from Theme.light to Theme.holo.light, the last line of the TextView gets cut in half. This happens when the dynamic text is long and spans more than one row.

enter image description here

I have been able to fix this by adding bottom padding to the horizontal LinearLayout containing the TextView and Spinner.

This does not feel like a fix, but more of a hack. Can someone please give me some advice on how to properly fix this?

I have also read some other questions, but none seem to help.

Horizontal Linear layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:text="TextView"/>

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Relative layout where above layout is dynamically inflated at Linear Layout with id ll2_7:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relLayoutButtonNext"
        android:layout_below="@id/textView1" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp" >

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="30dp"
                android:text="2.7" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_toRightOf="@id/textView10"
                android:text="@string/question2_7" />


            <LinearLayout
                android:id="@+id/ll2_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/textView11"
                android:layout_below="@+id/textView11"
                android:orientation="vertical" android:layout_marginBottom="20dp">
            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

EDIT:
Here is the complete layout xml for above:

How to&Answers:

I’ve encountered the same cut-off issue as shown at the screenshot. It is caused by the baseline alignment in the horizontal LinearLayout. TextView and Spinner have different baselines due to font size difference. To fix the issue it is needed to disable baseline alignment for the layout by setting:

or in the code:

Answer:

After trying a million different things, I think I have the answer.

I applied a LayoutGravity to the TextView item:

Seems to solve all clipping issues I had. Hope this helps someone with the same problem.

Answer:

I had the same problem, and found that simply adding

the final line of text no longer had its descenders clipped.

Answer:

I added some dummy space after text by adding

I tried all other solution.But this was the only solution worked for me

Answer:

Put the problematic textview inside a framelayout. I think the text view is not calculated correctly because of the sibling view, Spinner.

Answer:

When this occurs, you should ensure that the TextView is not growing larger than it’s container –

If a TextView is set to wrap_content and it’s container (or an ancestor container) doesn’t leave room for the TextView to grow into it can be occluded.

If that’s not the case, it’s also possible the onMeasure() of the TextView sometimes doesn’t correctly measure the tails of letters, non-latin characters or the effects from text being italic. You can correct for this by setting a global style for your TextView so it will be picked up without needed to change your entire code base:

Ensure that you’re application/activities use a custom theme like so:

The answer by @Rynadt was really helpful in getting to the above stage. Setting the gravity of the Text inside the View ensures on some devices that occlusion never takes place (The text is correctly fitted inside the view), on others a helping hand with padding of an sp value, ensures that the tails et al are accounted for with a TextSize specific value.

Answer:

My solution was close to the accepted one, but I had to change it to

instead. Otherwise the other rows would have been stretch as well with added line breaks at random places. For example, the biggest row had 4 lines, so another row was changed from

this is a testphrase

to

Answer:

I found a different solution by extending TextView and adding a custom Class like this:

Answer:

try with removing android:paddingBottom=”20dp”

from

Answer:

getViewTreeObserver().addOnGlobalLayoutListener does not work in a recycler view. If you’re using a recycler, use View.addOnLayoutChangeListener:

I found that the ellipsizing I defined for textView in xml was not always reflected so I programmatically set it before reassigning the text property. This worked for me.

Answer:

If you have this problem and your TextView is inside a RelativeLayout, try switching the RelativeLayout for a LinearLayout.

That fixed the problem for me

Answer:

You can use a global layout listener for a TextView in any type of ViewGroup.

You can read more about it here

Answer:

I think there is very little you can do to get this working by altering the layouts. As I have found that some methods work only in some cases. I think it depends on the entire layout hierarchy and is not a one-size-fits-all solution. I have also noticed that it happens especially when you have a different font that you want to set to the TextView.

A sure shot method that I have experimented and tested is that you can set the font attributes in code after the view is inflated. I am assuming that you have a font in the assets/fonts folder that you want to you.

For eg in a Fragment:

And in an Activity:

Answer:

I have this same problem, and its very annoying.

It only happens with Arabic text.

If you make the label multi-line and adding a \n at the end of your string, it would fix it, but the problem is that there would be a big gap between this label and the object below it, due to the fact that this field now has a new empty line below it.

A custom control can be done to get around that. But overall, this is an annoying bug.

Answer:

Best workaround for this is to add a dummy View of desired height (i.e. this will add padding itself) at the bottom of your view.

Like in my case I added one more table row at the bottom of the view. Hope this could help someone.

Answer:

I know it’s so late, but this is work like charm for me.
add this code to your textview

Answer:

Add padding to the bottom of the text view:

Answer:

I finally fixed it!

I try to add String to the TextView in Service and then call scrollTo(), the last line be cut off!

The scrollTo() should be call in “Runnable”, like:

I think it because in the monent of call scrollTo() in service, the update of TextView is not ready.