Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set max width in percentage of a TextView in LinearLayout of vertical orientation

I'd like to set the layout_weight of the TextView with the tv_long_text to 80% in the following LinearLayout of vertical orientation.

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
            android:id="@+id/tv_short_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            tools:text="short text" />
    <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.8"
            tools:text="a pretty long text" />
</LinearLayout>

The above is not working because the orientation of the textview's parent is vertical.

So, I tried to set the android:layout_width="match_parent" in the xml and then set the width at run time by getting the measured width and then sets the width to 80% but the getMeasuredWidth is giving me 0.

int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);

I also tried to set the layout_weight at run time but it didn't work either and it's probably because it's parent view is in vertical orientation.

longTextView.setLayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT,
                0.8f)
);

The one that worked for me is by adding some extra views for the long text view. But it's 2 more extra views added for just trying to set the width of this view in percentage. Is there any other efficient way to do this?

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
            android:id="@+id/tv_short_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            tools:text="short text" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.8"
            android:textStyle="bold"
            tools:text="a pretty long text" />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"/>
    </LinearLayout>
</LinearLayout>
like image 736
s-hunter Avatar asked Jan 25 '17 16:01

s-hunter


People also ask

What is the default value of the orientation attribute in LinearLayout?

android:orientation Use "horizontal" for a row, "vertical" for a column. The default is horizontal.

How do you align text in LinearLayout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”.

What is Layout_weight?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is LinearLayout?

Android LinearLayout is a view group that aligns all children in either vertically or horizontally.


1 Answers

I would not try to fiddle with run time measurements. Your solution with a second horizontal Layout is perfectly fine since you have two TextViews expanding horizontally.
Another option would be PercentRelativeLayout from the support library com.android.support:percent:25.1.0 See here

This is from the docs

 <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>
like image 173
Murat Karagöz Avatar answered Sep 18 '22 12:09

Murat Karagöz