title: Add dynamically components to a Custom Dialog in Android
body: <p>I'd need your help to do something in Android, in the following the use case.
  I have created a custom dialog in Android, whose layout is:
  <em>dialog_threshold.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="20dp"&gt;
      &lt;ImageView
          android:src="@drawable/ic_media_route_on_03_dark"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:scaleType="center"
          android:background="#FFFFBB33"
          android:contentDescription="@string/set_target" /&gt;
  
      &lt;Spinner
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/spinner_threshold" /&gt;
  
      &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/layout_threshold"&gt;
  
          &lt;TextView
              android:text=""
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/targetSelected"
              android:layout_weight="1" /&gt;
  
          &lt;Spinner
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/threshold_operator_spinner"
              android:layout_weight="1" /&gt;
  
          &lt;EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:inputType="textPersonName"
              android:text=""
              android:ems="10"
              android:id="@+id/threshold_val"
              android:layout_weight="1" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And in the Activity, when I click on a specific button, I have something like this, to create the custom dialog:</p>
  
  <pre><code>setThreshold_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  AlertDialog.Builder mBuilder = new AlertDialog.Builder(DiseaseActivity.this);
                  //Inflate the custom layout
                  View mView = getLayoutInflater().inflate(R.layout.dialog_threshold, null);
  
                  //Set title for dialog
                  mBuilder.setTitle("Set thresholds for your target");
                  //Define the spinner inside your custom layout
                  final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner_threshold); //because it doesn't exist in the main layout, but only in the custom layout
                  //Define the ArrayAdapter
                  ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(DiseaseActivity.this,
                          android.R.layout.simple_spinner_item,
                          getResources().getStringArray(R.array.threshold_choices));
                  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  mSpinner.setAdapter(adapter);
  
                  if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                      String ThresholdSelection = mSpinner.getSelectedItem().toString();
                  }
                  mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                      @Override
                      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                          if ((dialog2 != null) &amp;&amp; dialog2.isShowing() &amp;&amp; !mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                              dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
  
                          } else {
                              dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                          }
                      }
  
                      @Override
                      public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
                      }
                  });
  
                  //Set the positive and negative button for the custom dialog
                  mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          disease.setThreshold(mSpinner.getSelectedItem().toString());
  
                          if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
                              Toast.makeText(DiseaseActivity.this,
                                      mSpinner.getSelectedItem().toString(),
                                      Toast.LENGTH_SHORT)
                                      .show();
                              setInterval_button.setEnabled(true);
                          }
                      }
                  });
                  mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          dialogInterface.dismiss();
                      }
                  });
                  mBuilder.setView(mView);
                  dialog2 = mBuilder.create();
                  dialog2.show();
                  dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
  
              }
          });
  </code></pre>
  
  <p>What I want to do now is to add dynamically other components (like EditText or TextView) to the above Dialog before is it shown.
  How can I do it?</p>
  
  <p>Thanks in advance!</p>