title: Add imageview dynamically to existing view
body: <p>I'm developing an app which on start up will show a pre-defined layout like <strong>Image(1)</strong> in below screenshot.</p>
  
  <p>Now onclick of a button, I want to dynamically add another view like <strong>Image(2)</strong> in below screenshot to existing view resulting into some like <strong>Image(3)</strong> in below screenshot.</p>
  
  <p>If onclick is clicked again, <strong>Image(2)</strong> will be added to existing view resulting into something like <strong>Image(4)</strong>.</p>
  
  <p>How do I achieve this?<br> By searching, I found that it required something like <code>LayoutInflater.addView()</code> like <a href="https://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view">this</a> or <code>LinearLayout.addView()</code> like <a href="https://stackoverflow.com/questions/14002996/add-imageview-dynamically-to-view">this</a>.</p>
  
  <p>But I don't know what exactly to use in my case.<br>Also, I'm not trying to add <strong>just a single view</strong> on button click, but a group of certain views like imageview, 2 textviews,etc. as shown in <strong>Image(2)</strong>. </p>
  
  <p>Any help appreciated.</p>
  
  <hr>
  
  <p><img src="https://i.imgur.com/ss4KFIk.jpg" alt="screenshot"></p>
  
  <hr>
  
  <h2>Edit 1:</h2>
  
  <p>I tried something like this:
  <strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout 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" &gt;
  
          &lt;LinearLayout
              android:id="@+id/main"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" &gt;
  
              &lt;Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/hello_world" /&gt;
          &lt;/LinearLayout&gt;
  
      &lt;Button
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_marginBottom="20dp"
          android:onClick="addViews"
          android:text="Add" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
      LinearLayout main;
      int count = 0;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          main = (LinearLayout) findViewById(R.id.main);
      }
  
      public void addViews(View view) {
          LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                  LayoutParams.WRAP_CONTENT);
          Button btn = new Button(this);
          btn.setLayoutParams(lparams);
          count++;
          btn.setText("Hello World : " + count);
          main.addView(btn, count);
      }
  }
  </code></pre>
  
  <p>It yields something like this:</p>
  
  <p><img src="https://i.imgur.com/srxgodw.jpg" alt="screenshot"></p>
  
  <p><strong>Now, how do I recognize which button is clicked?</strong></p>