title: Loading dynamic layout in Android
body: <p>I'm currently making an application that uses a dynamic layout. It's my first time doing this and I'm doing it solely for the purpose of learning it. I managed to get it to load most of the data and show it correctly, but I'm having issues with the first item. For some reason it skips it and start with the 2nd item. It loads it into a variable but for some reason it isn't shown. I believe it has something to do with layout parameters but I can not find a reason for it behaving that way.</p>
  
  <p>This is how it looks (take note, it should start with 141- ... item. It is loaded into a variable tho but not shown)</p>
  
  <p><img src="https://i.stack.imgur.com/WlblT.png" alt="enter image description here"></p>
  
  <p>This is the code I'm using to load the items according to the variables</p>
  
  <pre><code>for (int i = 0; i &lt;= id_l; i++)
              {
                  RelativeLayout container = (RelativeLayout)findViewById(R.id.popisLinija);
                  View item_layout = getLayoutInflater().inflate(R.layout.item_layout, null);
                  Integer l = id_l;
                  Log.d("id length: ", l.toString());
                  Log.d("ID: ", id[i].toString());
                  Log.d("Linija br: ", broj_linije[i]);
                  Integer i2 = i;
                  Log.d("i= ", i2.toString());
  
                  item_layout.setId(i);
                  item_layout.setTag("View");
  
                  final Integer h = item_layout.getId();
  
                  // fill in any details dynamically here
                  assert item_layout != null;
                  TextView textView = (TextView) item_layout.findViewById(R.id.item_text);
  
                  if(broj_linije[i].equals("000"))
                  {
                      textView.setText(naziv_linije[i]);
                  }
                  else
                  {
                      textView.setText(broj_linije[i] + " - " + naziv_linije[i]);
                  }
  
  
                  textView.setTag("Text");
  
  
                  RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                  if(i == 0)
                  {
                      if (rlp != null) {
                          rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, container.getId());
                      }
                  }
                  if(i &gt; 0)
                  {
                      if (rlp != null) {
                          rlp.addRule(RelativeLayout.BELOW, i-1);
                      }
                  }
  
  
                  FrameLayout item_button = (FrameLayout) item_layout.findViewById(R.id.item_button);
                  item_button.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View arg0)
                      {
                          Integer a =  h;
                          Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show();
                          Log.d("clicked", h.toString());
                      }
                  });
  
                  item_layout.setLayoutParams(rlp);
                  container.addView(item_layout);
              }
  </code></pre>
  
  <p>I have checked if the item is loaded</p>
  
  <pre><code>&gt; 05-02 07:09:00.116  21204-21204/com.example.app D/ID:﹕ 0 05-02 &lt;&lt;&lt; ITEM 141 VARIABLE
  &gt; 07:09:00.116  21204-21204/com.example.app D/Linija br:﹕ 141 05-02 &lt;&lt;&lt; ITEM 141 VARIABLE
  &gt; 07:09:00.116  21204-21204/com.example.app D/i=﹕ 0 05-02 07:09:00.166  &lt;&lt;&lt; ITEM 141 VARIABLE
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.166 
  &gt; 21204-21204/com.example.app D/ID:﹕ 1 05-02 07:09:00.166 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 142 05-02 07:09:00.166 
  &gt; 21204-21204/com.example.app D/i=﹕ 1 05-02 07:09:00.176 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.176 
  &gt; 21204-21204/com.example.app D/ID:﹕ 2 05-02 07:09:00.176 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 143 05-02 07:09:00.176 
  &gt; 21204-21204/com.example.app D/i=﹕ 2 05-02 07:09:00.206 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.206 
  &gt; 21204-21204/com.example.app D/ID:﹕ 3 05-02 07:09:00.206 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 144 05-02 07:09:00.206 
  &gt; 21204-21204/com.example.app D/i=﹕ 3 05-02 07:09:00.216 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.216 
  &gt; 21204-21204/com.example.app D/ID:﹕ 4 05-02 07:09:00.216 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 145 05-02 07:09:00.216 
  &gt; 21204-21204/com.example.app D/i=﹕ 4 05-02 07:09:00.226 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.226 
  &gt; 21204-21204/com.example.app D/ID:﹕ 5 05-02 07:09:00.226 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 146 05-02 07:09:00.226 
  &gt; 21204-21204/com.example.app D/i=﹕ 5 05-02 07:09:00.246 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.246 
  &gt; 21204-21204/com.example.app D/ID:﹕ 6 05-02 07:09:00.246 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 147 05-02 07:09:00.246 
  &gt; 21204-21204/com.example.app D/i=﹕ 6 05-02 07:09:00.276 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.276 
  &gt; 21204-21204/com.example.app D/ID:﹕ 7 05-02 07:09:00.276 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 148 05-02 07:09:00.276 
  &gt; 21204-21204/com.example.app D/i=﹕ 7 05-02 07:09:00.316 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.316 
  &gt; 21204-21204/com.example.app D/ID:﹕ 8 05-02 07:09:00.316 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 149 05-02 07:09:00.316 
  &gt; 21204-21204/com.example.app D/i=﹕ 8 05-02 07:09:00.316 
  &gt; 21204-21208/com.example.app D/dalvikvm﹕ GC_CONCURRENT freed 949K, 37%
  &gt; free 7958K/12524K, paused 9ms+17ms, total 92ms 05-02 07:09:00.326 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.326 
  &gt; 21204-21204/com.example.app D/ID:﹕ 9 05-02 07:09:00.326 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 150 05-02 07:09:00.326 
  &gt; 21204-21204/com.example.app D/i=﹕ 9 05-02 07:09:00.336 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.336 
  &gt; 21204-21204/com.example.app D/ID:﹕ 10 05-02 07:09:00.336 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 150A 05-02 07:09:00.336 
  &gt; 21204-21204/com.example.app D/i=﹕ 10 05-02 07:09:00.346 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.346 
  &gt; 21204-21204/com.example.app D/ID:﹕ 11 05-02 07:09:00.346 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 151 05-02 07:09:00.346 
  &gt; 21204-21204/com.example.app D/i=﹕ 11 05-02 07:09:00.366 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.366 
  &gt; 21204-21204/com.example.app D/ID:﹕ 12 05-02 07:09:00.366 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 153 05-02 07:09:00.366 
  &gt; 21204-21204/com.example.app D/i=﹕ 12 05-02 07:09:00.376 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.376 
  &gt; 21204-21204/com.example.app D/ID:﹕ 13 05-02 07:09:00.376 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 155 05-02 07:09:00.376 
  &gt; 21204-21204/com.example.app D/i=﹕ 13 05-02 07:09:00.386 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.386 
  &gt; 21204-21204/com.example.app D/ID:﹕ 14 05-02 07:09:00.386 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 156 05-02 07:09:00.386 
  &gt; 21204-21204/com.example.app D/i=﹕ 14 05-02 07:09:00.396 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.396 
  &gt; 21204-21204/com.example.app D/ID:﹕ 15 05-02 07:09:00.396 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 157 05-02 07:09:00.396 
  &gt; 21204-21204/com.example.app D/i=﹕ 15 05-02 07:09:00.406 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.406 
  &gt; 21204-21204/com.example.app D/ID:﹕ 16 05-02 07:09:00.406 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 158 05-02 07:09:00.406 
  &gt; 21204-21204/com.example.app D/i=﹕ 16 05-02 07:09:00.416 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.416 
  &gt; 21204-21204/com.example.app D/ID:﹕ 17 05-02 07:09:00.416 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 159 05-02 07:09:00.416 
  &gt; 21204-21204/com.example.app D/i=﹕ 17 05-02 07:09:00.426 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.426 
  &gt; 21204-21204/com.example.app D/ID:﹕ 18 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 160 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/i=﹕ 18 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/ID:﹕ 19 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 162 05-02 07:09:00.436 
  &gt; 21204-21204/com.example.app D/i=﹕ 19 05-02 07:09:00.446 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.446 
  &gt; 21204-21204/com.example.app D/ID:﹕ 20 05-02 07:09:00.446 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 260 05-02 07:09:00.446 
  &gt; 21204-21204/com.example.app D/i=﹕ 20 05-02 07:09:00.456 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.456 
  &gt; 21204-21204/com.example.app D/ID:﹕ 21 05-02 07:09:00.456 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 260A 05-02 07:09:00.456 
  &gt; 21204-21204/com.example.app D/i=﹕ 21 05-02 07:09:00.476 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.476 
  &gt; 21204-21204/com.example.app D/ID:﹕ 22 05-02 07:09:00.476 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 261 05-02 07:09:00.476 
  &gt; 21204-21204/com.example.app D/i=﹕ 22 05-02 07:09:00.486 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.486 
  &gt; 21204-21204/com.example.app D/ID:﹕ 23 05-02 07:09:00.486 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 262 05-02 07:09:00.486 
  &gt; 21204-21204/com.example.app D/i=﹕ 23 05-02 07:09:00.496 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.496 
  &gt; 21204-21204/com.example.app D/ID:﹕ 24 05-02 07:09:00.496 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 263 05-02 07:09:00.496 
  &gt; 21204-21204/com.example.app D/i=﹕ 24 05-02 07:09:00.506 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.506 
  &gt; 21204-21204/com.example.app D/ID:﹕ 25 05-02 07:09:00.506 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 264 05-02 07:09:00.506 
  &gt; 21204-21204/com.example.app D/i=﹕ 25 05-02 07:09:00.516 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.516 
  &gt; 21204-21204/com.example.app D/ID:﹕ 26 05-02 07:09:00.516 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 265 05-02 07:09:00.516 
  &gt; 21204-21204/com.example.app D/i=﹕ 26 05-02 07:09:00.526 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.526 
  &gt; 21204-21204/com.example.app D/ID:﹕ 27 05-02 07:09:00.526 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 266 05-02 07:09:00.526 
  &gt; 21204-21204/com.example.app D/i=﹕ 27 05-02 07:09:00.536 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.536 
  &gt; 21204-21204/com.example.app D/ID:﹕ 28 05-02 07:09:00.536 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 267 05-02 07:09:00.536 
  &gt; 21204-21204/com.example.app D/i=﹕ 28 05-02 07:09:00.546 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.546 
  &gt; 21204-21204/com.example.app D/ID:﹕ 29 05-02 07:09:00.546 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.546 
  &gt; 21204-21204/com.example.app D/i=﹕ 29 05-02 07:09:00.556 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.556 
  &gt; 21204-21204/com.example.app D/ID:﹕ 30 05-02 07:09:00.556 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.556 
  &gt; 21204-21204/com.example.app D/i=﹕ 30 05-02 07:09:00.566 
  &gt; 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.566 
  &gt; 21204-21204/com.example.app D/ID:﹕ 31 05-02 07:09:00.566 
  &gt; 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.566 
  &gt; 21204-21204/com.example.app D/i=﹕ 31
  </code></pre>
  
  <p>Could anyone help me out on what am I doing wrong here? I would greatly appreciate it, I lost hours on this one trying all sorts of stuff with no result.</p>
  
  <p>EDIT:</p>
  
  <p>I have added my layout XML file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="wrap_content"
      android:background="#ffffff"
      android:id="@+id/item_container"
      android:clickable="false"
      android:focusable="false"&gt;
  
      &lt;RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="5dp"
          android:layout_marginLeft="5dp"
          android:layout_marginRight="5dp"
          android:padding="2dp"
          android:background="#e8e8e8"
          android:id="@+id/item_body"
          android:clickable="false"
          android:focusable="false"&gt;
  
          &lt;FrameLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@color/background"
              android:id="@+id/item_button"
              android:clickable="true"
              android:longClickable="true"
              android:focusable="false"
              android:focusableInTouchMode="false"
              android:minHeight="55dp"&gt;
  
                  &lt;FrameLayout
                      android:layout_width="3dp"
                      android:layout_height="fill_parent"
                      android:background="@android:color/transparent"
                      android:id="@+id/item_indicator"&gt;&lt;/FrameLayout&gt;
  
                  &lt;TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="141-SAMOBOR-RAKOV POTOK-JAGNJIĆ DOL"
                      android:id="@+id/item_text"
                      android:layout_gravity="center_vertical"
                      android:layout_marginLeft="8dp"
                      android:layout_marginRight="30dp"
                      android:textAppearance="@android:style/TextAppearance.Small"
                      android:gravity="fill" /&gt;
  
                  &lt;ImageView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:id="@+id/item_icon"
                      android:layout_gravity="center_vertical|right"
                      android:background="@android:color/transparent"
                      android:src="@drawable/plus_t"
                      android:layout_marginRight="5dp"
                      android:contentDescription="Proširi" /&gt;
  
              &lt;/FrameLayout&gt;
          &lt;/RelativeLayout&gt;
  
          &lt;RelativeLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:id="@+id/item_image_container"
              android:layout_below="@+id/item_body"
              android:layout_marginTop="5dp"
              android:background="#ffffff"&gt;
  
              &lt;ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/item_image"
                  android:background="@android:color/transparent"
                  android:layout_centerHorizontal="true"
                  android:scaleType="fitCenter" /&gt;
          &lt;/RelativeLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>