title: GridLayout collapses on small display
body: <p>I have <code>LinearLayout</code> with <code>TextView</code>, <code>GridLayout</code> and another <code>TextView</code>. It is displayed correctly on many modern phones and tablets but it utterly fails on small display like Nexus S. See following pictures.</p>
  
  <p><a href="https://i.stack.imgur.com/9iZF5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9iZF5.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/OTvUR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OTvUR.png" alt="enter image description here"></a></p>
  
  <p>I tried to decrease font size, remove 9-patch background and even remove all margins. I could see just digit7 when there were some margins. Then I minimized them and I can see four lines with their first button that is rendered twice as big as display. I do not understand why, it shall have <code>wrap_content</code> width.</p>
  
  <p>I tried to minimize the code and reduced it to <code>GridLayout</code> with single row and it still does not fit the width of the screen.</p>
  
  <p>What's going on there?</p>
  
  <p>Update:</p>
  
  <ul>
  <li>420x800 mdpi works </li>
  <li>420x800 hdpi is broken</li>
  <li>540x920 hdpi works</li>
  <li>720x1280 hdpi works</li>
  </ul>
  
  <p>Update 2:</p>
  
  <p>Nexus 5x has same issues when font is 38sp or bigger. It is interesting that Studio's designer shows correct layout but both device/emulator shows corrupted view and following error:</p>
  
  <pre><code>D/android.support.v7.widget.GridLayout: vertical constraints: y1-y0&gt;=197, y2-y1&gt;=197, y3-y2&gt;=197, y4-y3&gt;=197, y4-y0&lt;=785 are inconsistent; permanently removing: y4-y0&lt;=785. 
  </code></pre>
  
  <p>calc.xml:</p>
  
  <pre><code>&lt;LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"&gt;
  
  &lt;!--Display row--&gt;
  &lt;TextView
      android:id="@+id/assignment"
      android:text="50 + 40 = 90"
      style="@style/Formula"
      android:focusable="false"
      android:layout_width="match_parent"
      android:layout_height="80dp"
      tools:ignore="HardcodedText" /&gt;
  
  &lt;android.support.v7.widget.GridLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      app:columnCount="4"
      app:rowCount="4"&gt;
  
      &lt;!-- row with 7-9,+ --&gt;
  
      &lt;Button
          android:id="@+id/digit7"
          android:text="7"
          style="@style/KeypadLeftButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/digit8"
          android:text="8"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/digit9"
          android:text="9"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;Button
          android:id="@+id/buttonPlus"
          android:text="+"
          style="@style/KeypadRightButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_rowWeight="0.5"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;!--row with 4-6, - --&gt;
  
      &lt;Button
          android:id="@+id/digit4"
          android:text="4"
          style="@style/KeypadLeftButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;Button
          android:id="@+id/digit5"
          android:text="5"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;Button
          android:id="@+id/digit6"
          android:text="6"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/buttonMinus"
          android:text="-"
          style="@style/KeypadRightButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_rowWeight="0.5"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;!--row with 1-3, * --&gt;
  
      &lt;Button
          android:id="@+id/digit1"
          android:text="1"
          style="@style/KeypadLeftButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/digit2"
          android:text="2"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/digit3"
          android:text="3"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          app:layout_rowWeight="1"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;Button
          android:id="@+id/buttonMultiply"
          android:text="\u22C5"
          style="@style/KeypadRightButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_rowWeight="0.5"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;!--row with 0, backspace and / --&gt;
  
      &lt;Button
          android:id="@+id/digit0"
          android:text="0"
          style="@style/KeypadLeftButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_columnWeight="1"
          app:layout_gravity="fill_horizontal"
          app:layout_rowWeight="1"
          tools:ignore="HardcodedText"  /&gt;
  
      &lt;Button
          android:id="@+id/buttonBackspace"
          android:text="←"
          style="@style/KeypadButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_gravity="fill_vertical"
          app:layout_columnSpan="2"
          app:layout_rowWeight="0.5"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  
      &lt;Button
          android:id="@+id/buttonDivide"
          android:text=":"
          style="@style/KeypadRightButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_rowWeight="0.5"
          app:layout_columnWeight="1"
          tools:ignore="HardcodedText" /&gt;
  &lt;/android.support.v7.widget.GridLayout&gt;
  
  &lt;!--row with button submit --&gt;
  
  &lt;Button
      android:id="@+id/buttonSubmit"
      android:text="@string/action_next_formula"
      style="@style/KeypadNextButton"
      android:layout_width="match_parent"
      android:layout_height="80dp"
      app:layout_gravity="fill_horizontal"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>styles:</p>
  
  <pre><code>&lt;style name="Formula"&gt;
      &lt;item name="android:textSize"&gt;@dimen/calc_button_text&lt;/item&gt;
      &lt;item name="android:textColor"&gt;@color/gray_35&lt;/item&gt;
      &lt;item name="android:gravity"&gt;center&lt;/item&gt;
      &lt;item name="android:background"&gt;@drawable/lcd&lt;/item&gt;
      &lt;item name="android:layout_marginTop"&gt;@dimen/calc_big_margin&lt;/item&gt;
      &lt;item name="android:layout_marginBottom"&gt;@dimen/calc_superbig_margin&lt;/item&gt;
      &lt;item name="android:layout_marginLeft"&gt;@dimen/calc_big_margin&lt;/item&gt;
      &lt;item name="android:layout_marginRight"&gt;@dimen/calc_big_margin&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="KeypadButton" parent="@style/Widget.AppCompat.Button"&gt;
      &lt;item name="android:textSize"&gt;@dimen/calc_button_text&lt;/item&gt;
      &lt;item name="android:textColor"&gt;@color/white&lt;/item&gt;
      &lt;!--&lt;item name="android:background"&gt;@drawable/tl_2&lt;/item&gt;--&gt;
      &lt;item name="android:layout_marginLeft"&gt;@dimen/calc_small_margin&lt;/item&gt;
      &lt;item name="android:layout_marginBottom"&gt;@dimen/calc_small_margin&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="KeypadLeftButton" parent="@style/KeypadButton"&gt;
      &lt;item name="android:layout_marginLeft"&gt;@dimen/calc_big_margin&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="KeypadRightButton" parent="@style/KeypadButton"&gt;
      &lt;item name="android:layout_marginRight"&gt;@dimen/calc_big_margin&lt;/item&gt;
  &lt;/style&gt;
  
  &lt;style name="KeypadNextButton" parent="@style/Widget.AppCompat.Button.Colored"&gt;
      &lt;item name="android:background"&gt;@drawable/tl_next&lt;/item&gt;
      &lt;item name="android:textSize"&gt;@dimen/calc_button_text&lt;/item&gt;
      &lt;item name="android:layout_marginTop"&gt;@dimen/calc_big_margin&lt;/item&gt;
      &lt;item name="android:layout_marginBottom"&gt;@dimen/calc_big_margin&lt;/item&gt;
      &lt;item name="android:layout_marginLeft"&gt;@dimen/calc_big_margin&lt;/item&gt;
      &lt;item name="android:layout_marginRight"&gt;@dimen/calc_big_margin&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p>dimens:</p>
  
  <pre><code>&lt;dimen name="calc_button_text"&gt;14sp&lt;/dimen&gt;
  &lt;dimen name="calc_superbig_margin"&gt;2dp&lt;/dimen&gt;
  &lt;dimen name="calc_big_margin"&gt;1dp&lt;/dimen&gt;
  &lt;dimen name="calc_small_margin"&gt;0dp&lt;/dimen&gt;
  </code></pre>
  
  <p><strong>Update 16th feb</strong></p>
  
  <p>Manjunath Prabhakar said that the trouble may be related to weights. So I tried to remove all weight attributes and the layout makes more sense now. I have not fixed it yet though. I would consider rewriting the layout to nested LinearLayouts as a step back therefore I make bounty open for other suggestions.</p>
  
  <p><a href="https://i.stack.imgur.com/5Yrh4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5Yrh4.png" alt="enter image description here"></a></p>