title: Android AlertDialog with custom view: get input data
body: <p>I have an application with an AlertDialog that shows a single EditText. I followed the Android developers guide to do it, but I can't find how to get the data entered by the user.</p>
  
  <p>The custom layout only has an EditText:</p>
  
  <pre><code>&lt;EditText
      android:id="@+id/license_value"
      android:inputType="text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="@string/license" /&gt;
  </code></pre>
  
  <p>I'm using a DialogFragment to create the dialog. To get the data back when the user clicks the ok button I'm using an interface.</p>
  
  <pre><code>public class EditLicenseDialogFragment extends DialogFragment {
  
      public interface EditLicenseDialogListener {
          public void onDialogPositiveClick(DialogFragment dialog, String value);
      }
  
      private EditLicenseDialogListener mListener;
  
      // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          // Verify that the host activity implements the callback interface
          try {
              // Instantiate the NoticeDialogListener so we can send events to the host
              mListener = (EditLicenseDialogListener) activity;
          } catch (ClassCastException e) {
              // The activity doesn't implement the interface, throw exception
               throw new ClassCastException(activity.toString()
                      + " must implement NoticeDialogListener");
          }
      }
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          // Use the Builder class for convenient dialog construction
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  
          builder.setTitle(R.string.new_license);
  
          LayoutInflater inflater = getActivity().getLayoutInflater();
  
          builder.setView(inflater.inflate(R.layout.edit_license, null))
  
          .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int id) {
                          // GET VALUE. HOW TO DO IT?
                  EditText valueView = (EditText) getActivity().findViewById(R.id.license_value);
                  if(valueView == null) Log.d("AA", "NULL");
                  else{
                      String value = valueView.getText().toString();
                      mListener.onDialogPositiveClick(EditLicenseDialogFragment.this, value);
                  }
              })
          .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                  EditLicenseDialogFragment.this.getDialog().cancel();
              }
          }); 
  
          return builder.create();
  
      }
  }
  </code></pre>
  
  <p>In my activity I do the following:</p>
  
  <pre><code>public class LicenseListActivity extends FragmentActivity 
      implements EditLicenseDialogFragment.EditLicenseDialogListener{
  
      ...
  
      findViewById(R.id.buttonAddLicense).setOnClickListener(
              new View.OnClickListener() {
                  public void onClick(View view) {
                      DialogFragment dialog = new EditLicenseDialogFragment(true);
                      dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");
                  }
              });
  
      @Override
      public void onDialogPositiveClick(DialogFragment dialog, String value) {
         Log.d("TAG", value);
      }
  </code></pre>
  
  <p>The EditText I try to retrieve inside the DialogFragment is always NULL. How can I get the value of the EditText?</p>
  
  <p>Thanks!</p>