title: How can I add images to a LinearLayout programmatically?
body: <p>There is a LinearLayout and some Buttons in my project. I want when each Button clicked add a small image to LinearLayout from drawable. How can I add a image to a LinearLayout programmatically?</p>
  
  <p>This is my LinearLayout in a HorizontalScrollView:</p>
  
  <pre><code>     &lt;HorizontalScrollView
          android:id="@+id/horizontalScrollView1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="top|center"
          &gt;
  
          &lt;LinearLayout
              android:id="@+id/LinearLayout1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal" &gt;
  
  
          &lt;/LinearLayout&gt;
      &lt;/HorizontalScrollView&gt;
  </code></pre>
  
  <p>and here is my activity:</p>
  
  <pre><code>public class MainActivity extends Activity
  {
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.activity_main);
  
              final LinearLayout notes = (LinearLayout)  findViewById(R.id.LinearLayout_notes);
              final  ImageView notes_do = new ImageView(this);
              notes_do.setBackgroundResource(R.drawable.notes_do);
  
      new Thread(new Runnable() {
          @Override
          public void run() {
  
      final ImageButton img_1 = (ImageButton) findViewById(R.id.img_1_);
      img_1.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
  
          if( event.getAction() ==  MotionEvent.ACTION_DOWN ) {
  
                notes.addView(notes_do);
  
          }
              return true;
          } 
  
         });  // end of ontouch
  
             }
            }).start(); // end of thread      
  
  
  } // end of oncreate
  }// end of activity
  </code></pre>