5

I am trying to use GridLayout and am having trouble expanding the rows (with TextViews) to "fill-up" the space in the layout.

Vertical Picture (note red line is where rows should expand): enter image description here

Horizontal Picture:
enter image description here

main.xml:

<?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="match_parent"
  android:columnCount="2"
  android:useDefaultMargins="true" >
<TextView
    style="@android:style/TextAppearance.Large"
    android:layout_columnSpan="2"
    android:layout_gravity="center_horizontal"
    android:padding="8dp"
    android:text="Which means: lacking in originality as to be obvious and boring." />
<Space android:layout_height="24dp" android:layout_columnSpan="2" />
<TextView style="@style/AnswerLetter" android:text="A." />
<TextView style="@style/AnswerChoice" android:text="ascetic" />
<TextView style="@style/AnswerLetter" android:text="B." />
<TextView style="@style/AnswerChoice" android:text="banal" />
<TextView style="@style/AnswerLetter" android:text="C." />
<TextView style="@style/AnswerChoice" android:text="contraption" />
<TextView style="@style/AnswerLetter" android:text="D." />
<TextView style="@style/AnswerChoice" android:text="dominion" />
<TextView style="@style/AnswerLetter" android:text="E." />
<TextView style="@style/AnswerChoice" android:text="enervation" />
</GridLayout>

styles.xml:

 <style name="AnswerLetter" parent="@android:style/TextAppearance.Medium">
   <item name="android:layout_gravity">right</item>
   <item name="android:paddingLeft">32dp</item>
   <item name="android:textStyle">bold</item>
   <item name="android:gravity">center_vertical|center_horizontal</item>
 </style>
 <style name="AnswerChoice" parent="@android:style/TextAppearance.Large">
   <item name="android:layout_gravity">left</item>
   <item name="android:paddingLeft">32dp</item>
   <item name="android:gravity">left</item>
 </style>
Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
AG1
  • 6,648
  • 8
  • 40
  • 57

2 Answers2

6

You want all your child subviews to grow (in height) in order to fill out that extra space, but that's not possible inside a GridLayout:

GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns ... For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group."

Try adding a linear layout as a subview and setting the weight attributes of all children to the same value (so they expand equally).

Similar question: GridLayout (not GridView) how to stretch all children evenly

Community
  • 1
  • 1
Victor
  • 172
  • 1
  • 11
3

If i understand you correctly - this layout can solve your problem. GridView can't strech column hight, so you can use LinearLayout. I make this layout for you, maybye it needs some padding adjustments

horisontal vertial

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/LinearLayout1"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        style="@android:style/TextAppearance.Large"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp"
        android:text="Which means: lacking in originality as to be obvious and boring." />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="A." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="ascetic" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            style="@style/AnswerLetter"
            android:text="B." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="banal" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="C." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="contraption" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="D." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="dominion" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            style="@style/AnswerLetter"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:text="E." />
        <TextView
            style="@style/AnswerChoice"
            android:layout_width="match_parent"
            android:gravity="center_vertical"
            android:layout_height="match_parent"
            android:text="enervation" />
    </LinearLayout>
</LinearLayout>
Berezovskyi
  • 364
  • 3
  • 5