Skip to content Skip to sidebar Skip to footer

Textview With Long Text Pushes Out Other Views In Gridlayout Despite Ellipsize=end

My problem is very similar to How to get a layout where one text can grow and ellipsize, but not gobble up the other elements on the layout, but read on below why I can't use Table

Solution 1:

I seem to have found a potential solution to prevent a TextView in GridLayout from growing unboundedly and pushing out other views. Not sure if this has been documented before.

You need to use fill layout_gravity and set an arbitrary layout_width or width on the long TextView in need of ellipsizing.

android:layout_gravity="fill"android:layout_width="1dp"

Works for both GridLayout and android.support.v7.widget.GridLayout

Solution 2:

I'm a big fan of LinearLayouts, so here's my suggestion using those:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ellipsize="end"android:singleLine="true"android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="(view1)" /></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:text="(view2)" /></LinearLayout>

Solution 3:

I think you should create custom layout for your purpose. I don't know how to do this using only default layouts/view and make it work for all cases.

Solution 4:

I will suggest you to play with layout_weight property of your widget

Example:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:weightSum="10"><LinearLayoutandroid:id="@+id/ll_twoViewContainer"android:layout_weight="8"android:layout_width="0dp"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/rl0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_weight="1"android:ellipsize="end"android:singleLine="true"android:text="Long text" /><TextViewandroid:id="@+id/rl1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/rl0"android:layout_weight="1"android:minWidth="120dp"android:text="(view1)" /></LinearLayout><TextViewandroid:id="@+id/rl2"android:layout_weight="2"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/rl1"android:layout_alignParentRight="true"android:layout_marginLeft="10dp"android:text="(view2)" /></LinearLayout></LinearLayout>

finally your layout will look like as follow:

+----------------------------------------+
| shorttext [view1]             [view2] |
+----------------------------------------+
| longtextwith ell ... [view1] [view2] |
+----------------------------------------+

Solution 5:

The trick which worked for me was to use maxWidth to restrict the width of the first view. You need to do it with Java, here is the basic logic:

firstView.setMaxWidth(parentView.getWidth() - view2.getWidth() - view1.getWidth() - padding * 2);

Not pretty, but it works.

Post a Comment for "Textview With Long Text Pushes Out Other Views In Gridlayout Despite Ellipsize=end"