title: How to create a dynamic array button in Android?
body: <p>I have a button.  I want to write the function "onClick" for the button.
  Each time that I click on it I want a new button to be created on screen.
  And also that new button to be added into an array list of buttons.</p>
  
  <p>I searched but I couldn't find any answer. All of the answers talk about creating buttons with drop and drag, not with dynamic creation of them.</p>
  
  <p>Thanks</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Click to add"
          android:id="@+id/B1"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:onClick="clicktoaddcode"
          /&gt;
  &lt;/RelativeLayout&gt;
  
  package com.example.yas.dynamicbutton;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      public void clicktoaddcode(View V){
          Toast.makeText(MainActivity.this, "Created", Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>