title: Android: Dynamically populating GridLayout
body: <p>I'm trying to create an activity where I have a GridLayout that is populated dynamically:</p>
  
  <p>Here's the xml layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns: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"
      android:gravity="center"
      android:orientation="vertical"
      tools:context="GoalsActivity" &gt;
  
      &lt;ScrollView
          android:id="@+id/scroll_view"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" &gt;
  
          &lt;GridLayout
              android:id="@+id/goals_grid"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:columnCount="2"
              android:orientation="horizontal"
              android:layout_gravity="center" &gt;
          &lt;/GridLayout&gt;
      &lt;/ScrollView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And here's the activity:</p>
  
  <pre><code>public class GoalsActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_goals);
          try {
              JSONArray goals = new JSONArray(getIntent().getStringExtra("GOAL_LIST"));
              GridLayout goalsGrid = (GridLayout) findViewById(R.id.goals_grid);
  
              for(int i = 0; i &lt;= goals.length(); i++) {
                  JSONObject goal = goals.getJSONObject(i);
                  TextView goalView = new TextView(GoalsActivity.this);
                  goalView.setHeight(125);
                  goalView.setWidth(125);
                  goalView.setText(goal.getString("name"));
                  goalsGrid.addView(goalView);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>As you can see I'm trying to display the goals using a GridLayout but whenever I run this code, the following happens:</p>
  
  <p><a href="https://i.stack.imgur.com/K8rNN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K8rNN.png" alt="GirdLayout problem"></a></p>
  
  <p>Apparently all TextViews are stacking on top of each other, but if I add 2 of these:</p>
  
  <pre><code>&lt;TextView
      android:height="125dp"
      android:width="125dp" /&gt; 
  </code></pre>
  
  <p>under the GridLayout in my xml layout manually, they show up just like I need them to:</p>
  
  <p><a href="https://i.stack.imgur.com/CX7rx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CX7rx.png" alt="Grid size"></a></p>
  
  <p><a href="https://i.stack.imgur.com/CXlgS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CXlgS.png" alt="TextView size"></a></p>
  
  <p>I can't figure out how to fix this, any help is greatly appreciated!</p>