title: how to dynamically change the position of buttons
body: <p>I have a <code>MainActivity</code> initially empty with an action bar. Clicking on the "plus" in the action bar,I open a <code>DialogFragment</code> with an <code>EditText</code> and two buttons, "cancel" and "continue". I can not post a picture because I do not have enough reputation, but when I click on "continue" I create two buttons; one positioned at the top right of the layout of <code>MainActivity</code> and the other, just below the previous one, on the left. What I would like is that every time I click on "continue", I can create two buttons positioned according to the same pattern that I explained earlier.</p>
  
  <p>This is the code I've written but it does not work as I would like.</p>
  
  <p>MainActivity</p>
  
  <pre><code>public class MainActivity extends FragmentActivity implements IProjectDialFrag {
  
      private ProjectDialogFragment projectDialFrag = new ProjectDialogFragment();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
              inflater.inflate(R.menu.main, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          switch (id) {
              case R.id.action_settings:
                  //TODO
                  return true;
              case R.id.filters:
                  Intent intent1 = new Intent(MainActivity.this, FiltersActivity.class);
                  startActivity(intent1);
                  return true;
              case R.id.action_new:
                  return true;
              case R.id.add_button:
                  Intent intent2 = new Intent(MainActivity.this, ButtonsActivity.class);
                  startActivity(intent2);
                  return true;
              case R.id.add_project:
                  projectDialFrag.show(getFragmentManager(), "projectDialog");
                  return true;
              default:
                  return super.onOptionsItemSelected(item);
          }
  
      }
  
      @Override
      public void onDialogNegativeClick(DialogFragment dialog) {
          return;
      }
  
      @Override
      public void onDialogPositiveClick(DialogFragment dialog) { 
          EditText editText = (EditText) dialog.getDialog().findViewById(R.id.project_name);
          String projectName = editText.getText().toString();
  
          Button projectButton = new Button(this);
              projectButton.setText(projectName);
  
          ImageButton playButton = new ImageButton(this);
              playButton.setImageResource(R.drawable.ic_action_play_over_video);
  
          LinearLayout linearLayout = (LinearLayout) findViewById(R.id.button_row);
              linearLayout.addView(projectButton);
              linearLayout.addView(playButton);
      }
  
  }
  </code></pre>
  
  <p>The Interface</p>
  
  <pre><code>public interface IProjectDialFrag {
  
      public void onDialogPositiveClick(DialogFragment dialog);
      public void onDialogNegativeClick(DialogFragment dialog);
  
  }
  </code></pre>
  
  <p>And the DialogFragment</p>
  
  <pre><code>public class ProjectDialogFragment extends DialogFragment {
  
      private IProjectDialFrag iProjDialFrag;
  
      @SuppressLint("InflateParams")
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
           AlertDialog.Builder createProjectAlert = new AlertDialog.Builder(getActivity());
  
           createProjectAlert.setTitle("Create Project");
  
           LayoutInflater inflater = getActivity().getLayoutInflater();
  
           createProjectAlert.setView(inflater.inflate(R.layout.project_dialog_fragment, null))
  
              .setPositiveButton(R.string.conti_nue, new DialogInterface.OnClickListener() {
  
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                      iProjDialFrag.onDialogPositiveClick(ProjectDialogFragment.this);
                  }
              })
              .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                      iProjDialFrag.onDialogNegativeClick(ProjectDialogFragment.this);
  
                  }
              });
  
           return createProjectAlert.create();
  
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          iProjDialFrag = (IProjectDialFrag) activity;
      }
  
  }
  </code></pre>
  
  <p>The layout of MainActivity </p>
  
  <pre><code>&lt;RelativeLayout 
  
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
  
      android:id="@+id/main_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      android:orientation="horizontal" &gt; 
  
      &lt;LinearLayout
          android:id="@+id/button_row"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_alignParentTop="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true" &gt;
      &lt;/LinearLayout&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>If you need a screenshot, please give me a point reputation and I'll place it immediately. Than you</p>
  
  <p>Thank you again. Here is my screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/GzXES.png" alt="enter image description here"></p>
  
  <p>This is the result I'd like to have:</p>
  
  <p><img src="https://i.stack.imgur.com/mshHb.jpg" alt="enter image description here"></p>
  
  <p>For each Button I have to be able to add more "play".</p>