Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Last line of textview cut off

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

I applied a LayoutGravity to the TextView item:

android:layout_gravity="fill"

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


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:

android:baselineAligned="false"

or in the code:

layout.setBaselineAligned(false);

I had the same problem, and found that simply adding

android:includeFontPadding="false"

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


I added some dummy space after text by adding

textView.setText(firstString+"\n");

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


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

 public class AdaptingTextView extends TextView {

    public AdaptingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public AdaptingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AdaptingTextView(Context context) {
        super(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // set fitting lines to prevent cut text
        int fittingLines = h / this.getLineHeight();
        if (fittingLines > 0) {
            this.setLines(fittingLines);
        }
    }

}

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

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

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