title: Create new FrameLayout on button click - Android
body: <p>I'm trying to create a new FrameLayout each time I click a button. At the moment I've manually implemented 3 FrameLayouts and when I click the button all 3 are set to visible. All I want for now is for one FrameLayout to be set to visible, then if the button is clicked again, the second FrameLayout to appear etc.. The java code:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      public final static String EXTRA_MESSAGE = "com.example.viginti.MESSAGE";
      int hoursValue;
      int minutesValue;
      int finalMinutes;
      int finalHours;
      int correctDivision;
      int timeLeft;
      int twentyFour;
      int i, x;
      NumberPicker np_hours;
      NumberPicker np_minutes;
  
      @Override
      protected void onCreate( Bundle savedInstanceState ) {
          super.onCreate( savedInstanceState );
          setContentView( R.layout.activity_main );
  
          fillArray();
      }
  
      @Override
      public boolean onCreateOptionsMenu( Menu menu ) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate( R.menu.main, menu );
          return true;
      }
  
      public void generateAnswer( View view ) {
  
          hoursValue = np_hours.getValue();
          minutesValue = np_minutes.getValue();
  
          try {
              twentyFour = 24;
              correctDivision = 10;
              finalMinutes = minutesValue / correctDivision;
              finalHours = twentyFour - hoursValue;
              timeLeft = finalHours - finalMinutes;
          } catch( NumberFormatException e ) {
              /** DEBUGGING */
              System.out.println( "Number Format Exception: " + e );
          }
          String finalResult = Integer.toString( timeLeft );
          Intent displayData = new Intent( this, DisplayData.class );
          displayData.putExtra( EXTRA_MESSAGE, finalResult );
          startActivity( displayData );
      }
  
      public void fillArray() {
          np_hours = ( NumberPicker ) findViewById( R.id.hourNumber );
          np_minutes = ( NumberPicker ) findViewById( R.id.minuteNumber );
          String[] hoursArray = new String[25];
          String[] minutesArray = new String[61];
          for( i = 0; i &lt; hoursArray.length; i++ ) {
              hoursArray[i] = Integer.toString( i );
          }
  
          for( x = 0 ; x &lt; minutesArray.length; x++ ){
              minutesArray[x] = Integer.toString( x );
          }
          np_hours.setMinValue( 0 );
          np_hours.setMaxValue( 24 );
          np_hours.setWrapSelectorWheel( false );
          np_hours.setDisplayedValues( hoursArray );
  
          np_minutes.setMinValue( 0 );
          np_minutes.setMaxValue( 60 );
          np_minutes.setWrapSelectorWheel( false );
          np_minutes.setDisplayedValues( minutesArray );
      }
  
      @SuppressLint("NewApi")
      public void adActivity( View view ) {
  
          //Add first frame
          FrameLayout addActivities = ( FrameLayout )findViewById( R.id.frameLayout2 );
          addActivities.setVisibility( View.VISIBLE );
  
          //Add second frame
          FrameLayout addActivities1 = ( FrameLayout )findViewById( R.id.frameLayout3 );
          addActivities1.setVisibility( View.VISIBLE );
          //Add third frame
          FrameLayout addActivities2 = ( FrameLayout )findViewById( R.id.frameLayout4 );
          addActivities2.setVisibility( View.VISIBLE );
          fillArray();
      }
  }
  </code></pre>
  
  <p>The XML:</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"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".MainActivity" &gt;
  
  
      &lt;ImageView
          android:id="@+id/featuredimage"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="18dp"
          android:clickable="true"
          android:onClick="generateAnswer"
          android:scaleType="centerCrop"
          android:src="@drawable/generate_button" /&gt;
  
        &lt;ImageView
          android:id="@+id/addActivity"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_marginBottom="50dp"
          android:clickable="true"
          android:onClick="adActivity"
          android:src="@drawable/add_activity" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/frameLayout1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"&gt;
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal" &gt;
  
              &lt;Spinner
                  android:id="@+id/plan"
                  android:layout_width="121dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="0dp"
                  android:layout_marginTop="15dp"
                  android:entries="@array/daily_plans" /&gt;
  
              &lt;NumberPicker
                  android:id="@+id/hourNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="24dp"/&gt;
  
              &lt;NumberPicker
                  android:id="@+id/minuteNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="10dp" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/FrameLayout&gt;
  
      &lt;FrameLayout
          android:id="@+id/frameLayout2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/frameLayout1"
          android:visibility="gone"&gt;
  
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal" &gt;
  
              &lt;Spinner
                  android:id="@+id/plan1"
                  android:layout_width="121dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="0dp"
                  android:layout_marginTop="15dp"
                  android:entries="@array/daily_plans" /&gt;
  
              &lt;NumberPicker
                  android:id="@+id/hourNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="24dp"/&gt;
  
              &lt;NumberPicker
                  android:id="@+id/minuteNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="10dp" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/FrameLayout&gt;
      &lt;FrameLayout
          android:id="@+id/frameLayout3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/frameLayout2"
          android:visibility="gone"&gt;
  
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal" &gt;
  
              &lt;Spinner
                  android:id="@+id/plan1"
                  android:layout_width="121dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="0dp"
                  android:layout_marginTop="15dp"
                  android:entries="@array/daily_plans" /&gt;
  
              &lt;NumberPicker
                  android:id="@+id/hourNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="24dp"/&gt;
  
              &lt;NumberPicker
                  android:id="@+id/minuteNumber"
                  android:layout_width="40dp"
                  android:layout_height="70dp"
                  android:layout_marginLeft="10dp" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/FrameLayout&gt;
      &lt;FrameLayout
              android:id="@+id/frameLayout4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/frameLayout3"
              android:visibility="gone"&gt;
  
  
              &lt;LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="horizontal" &gt;
  
                  &lt;Spinner
                      android:id="@+id/plan1"
                      android:layout_width="121dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="0dp"
                      android:layout_marginTop="15dp"
                      android:entries="@array/daily_plans" /&gt;
  
                  &lt;NumberPicker
                      android:id="@+id/hourNumber"
                      android:layout_width="40dp"
                      android:layout_height="70dp"
                      android:layout_marginLeft="24dp"/&gt;
  
                  &lt;NumberPicker
                      android:id="@+id/minuteNumber"
                      android:layout_width="40dp"
                      android:layout_height="70dp"
                      android:layout_marginLeft="10dp" /&gt;
              &lt;/LinearLayout&gt;
          &lt;/FrameLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Worth mentioning that I'm fairly new to Android development. Also, if there is a better way of generating FrameLayouts each time the button is clicked (and positioning them correctly) I'd love to hear!</p>
  
  <p>Thanks a lot for any help!</p>