12

I have a problem using GridLayout using library compatibility (not tried without). I am using app:layout_gravity="fill_horizontal" instead of android:layout_gravity="fill_horizontal" but all the content inside the TextView is not displayed. In order to display everything, I have to set the height of the TextView "Title" but I want a dynamic height, not a set height.

Any idea?

Matt
  • 74,352
  • 26
  • 153
  • 180
Jul
  • 1,039
  • 3
  • 12
  • 20

3 Answers3

35

You have to set layout_width="0dp" and layout_gravity="fill_horizontal" for the TextView.

<TextView
    android:layout_width="0dp"
    android:layout_gravity="fill_horizontal" />

Please, see full example here: https://groups.google.com/d/msg/android-developers/OmH3VBwesOQ/ZOGR0SGvC3cJ or here: http://daniel-codes.blogspot.com/2012/01/gridlayout-view-clipping-issues.html

t0m
  • 3,004
  • 31
  • 53
Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
  • Its important to note the **app:**layout_gravity. I was setting the value in a style and it lint did not warn me, took me a while to figure out the problem. – Anuj Feb 27 '15 at 07:43
  • It takes the width of previous row's width if it is situated in the 2nd row – swapnil saha Jun 08 '16 at 09:04
22

Using TextView inside GridLayout is problematic, but there is a good way to use both together.

This is what the example layout looks like:

TextView inside GridLayout

And this is the complete layout xml, the important lines are marked with ***.

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"              *   this example uses 3 columns
    android:orientation="horizontal" >   *** use "horizontal"

<TextView                                *   just a normal view
    android:layout_column="0"
    android:layout_row="0"
    android:background="#666666"
    android:text="A"
    android:textColor="#afafaf"
    android:textSize="60sp"
    android:textStyle="bold" />

<TextView                                *   this text will not be cut!
    android:layout_width="0dp"           *** important: set width to 0dp
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:layout_columnSpan="2"        *   colspan does also work with this
    android:layout_gravity="fill_horizontal|bottom"        *** set to "fill*"!
    android:layout_row="0"
    android:text="This view has 2 columns. Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
    android:textColor="#666666" />

</GridLayout>

Depending on your needs, also this combination will work:

    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_gravity="fill"
    android:gravity="bottom"

Note that you don't have to use any namespace other than android for this to work.

sulai
  • 5,204
  • 2
  • 29
  • 44
  • Thank you, it works. But I'm curious how did you discover that trick? (0dp for `layout_width` and fill properties for `layout_gravity`) – Leo Oct 28 '14 at 03:01
  • 2
    Great job finding that out. Had a hard time getting my TextView wrapping the text correctly inside my GridLayout. Followed all other suggestions and answers on many topics about this same issue but only yours worked. Thank you very much! :) – Diogo Mendonça Jun 18 '15 at 10:42
0

Use this:

  <TextView
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_columnWeight="1"
      android:layout_gravity="fill"
      android:layout_margin="10dp"
      android:gravity="center"
      android:text="any long text" />
AnkitRox
  • 534
  • 1
  • 6
  • 16