Search code examples
android-activityandroid-dialogfragment

DialogFragments and activities, passing info


I'm trying to show a DialogFragment asking the user his name, and I need to get back this string to my main activity.

I've created an custom dialog fragment following the android guide: http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

In my code, in response to the user click on positive button, I am able to call a function in my main activity:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.textdialog, null)).setPositiveButton("OK", new DialogInterface.OnClickListener(){
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doPositiveClick();
               }
           }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   ((mainActivity)getActivity()).doNegativeClick();
               }
           });      
    return builder.create();
}

where R.layout.textdialog is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<EditText
        android:id="@+id/textdraw"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="introduce text" />

</LinearLayout>

My question is, how could I get the text written by the user in my dialog fragment and send it back to my main activity?

thanks!


Solution

  • Well, I solved myself (but not sure if it's the better way)

    I've done some changes in my onCreateDialog.

    1. Save the view in a variable:

      View v= inflater.inflate(R.layout.textdialog, null);

    2. Save the edittext in a variable:

      final EditText ed=(EditText)v.findViewById(R.id.textdraw);

    3. Send back the value in edittext variable when the user clicks the positive button:

      getActivity()).doPositiveClick(ed.getText().toString());

    Full code is here:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View v= inflater.inflate(R.layout.textdialog, null);
        // reference to the edittext
        final EditText ed= (EditText)v.findViewById(R.id.textdraw);
    
    
        builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener(){
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       ((mainActivity)getActivity()).doPositiveClick(ed.getText().toString());
                   }
               }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       ((mainActivity)getActivity()).doNegativeClick();
                   }
               });      
        return builder.create();
    }
    

    and the code in main activity:

    public void doPositiveClick(String ed){
        Toast.makeText(this, "Hi, "+ed, Toast.LENGTH_SHORT).show();
    }