Sean Stayns
Sean Stayns

Reputation: 4234

LinearLayout cuts off TextView text by using weight property (height)

I want to make a design for every android device. For this, I use LinearLayouts with height and with it a percentage solution.

The screen is divided in many sections (LinearLayouts with weights). In these LinearLayouts are the elements, like a TextView.

But if I a LinearLayout with height could cut off the TextView on the bottom side.

How can I change the text size dynamically, based on the weights?

The code:

 <LinearLayout
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="0.04"
    android:weightSum="1">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.05799" />
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.86951">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="Benachrichtigungen"
            android:textStyle="bold"
            android:textSize="20sp"
            android:id="@+id/header"/>
    </LinearLayout>
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="0.0722106" />
</LinearLayout>

Image:

Image

Upvotes: 1

Views: 470

Answers (1)

Sean Stayns
Sean Stayns

Reputation: 4234

I could solve my problem programmatically:

This code is in the OnCreateView method in my fragment. It generates the text size based on the height of the TextView.

view.ViewTreeObserver.GlobalLayout += (sender, e) => 
{
    var headerTextView = view.FindViewById<TextView>(Resource.Id.header);
    var headerTextViewHeightInSp = PxToSp(view.Context, headerTextView.Height);

    headerTextView.SetTextSize(ComplexUnitType.Sp, headerTextViewHeightInSp - 5); // 5 is a offset (space between outter borderline and text)
};

Upvotes: 1

Related Questions