How to get a return value from a DialogFragment? or Using DialogFragments in android.

By | July 1, 2013

Hello all……

This post examples how to get an edittext value from a dialog fragment.
You can use the same method to get a button click from a dialog or events like that.

Dialog Fragment

You can click on the download link to get the source code.

At first we will create a layout for the Dialog Fragment.
The layout contains only an editext. We are getting this edittext value from the fragment to the activity when the “DONE” button is clicked after inputting text.

frag_edit_name.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/lbl_your_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:layout_margin="10dp"
        android:text="Your name" />

    <EditText
        android:id="@+id/txt_your_name"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:hint="Type your name"
        android:imeOptions="actionDone"
        android:inputType="text" />

</LinearLayout>

Next we will write the class for the DialogFragment.

MyDialogFragment is my class name for the DialogFragment.

MyDialogFragment.java

package com.example.dialogfragmentsexxample;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MyDialogFragment extends DialogFragment implements
		OnEditorActionListener {

	public interface EditNameDialogListener {
		void onFinishEditDialog(String inputText);
	}

	private EditText mEditText;

	public MyDialogFragment() {
		// Empty constructor required for DialogFragment
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.frag_edit_name, container);
		mEditText = (EditText) view.findViewById(R.id.txt_your_name);
		getDialog().setTitle("Dialog Fragment Example");

		// Show soft keyboard automatically
		mEditText.requestFocus();
		mEditText.setOnEditorActionListener(this);

		return view;
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (EditorInfo.IME_ACTION_DONE == actionId) {
			// Return input text to activity
			EditNameDialogListener activity = (EditNameDialogListener) getActivity();
			activity.onFinishEditDialog(mEditText.getText().toString());
			this.dismiss();
			return true;
		}
		return false;
	}
}

OK Now our fragment is complete.
Here we use interface to get value from the editext in the DialogFragment.

This is the interface which has only one method

onFinishEditDialog

Now we implement this interface in the activity and call the method “onFinishEditDialog” from the dialog after inputting the text.
This is the line that calls this function from the dialogfragment.

activity.onFinishEditDialog(mEditText.getText().toString());

This is the activity class.

MainActivity.java

package com.example.dialogfragmentsexxample;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.TextView;

import com.example.dialogfragmentsexxample.MyDialogFragment.EditNameDialogListener;

public class MainActivity extends FragmentActivity implements
		EditNameDialogListener {

	TextView txt = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		txt = new TextView(this);
		txt.setText("Nothing received from Dialog Fragment");
		txt.setTextSize(30);
		setContentView(txt);
		showEditDialog();
	}

	private void showEditDialog() {
		FragmentManager fm = getSupportFragmentManager();
		MyDialogFragment editNameDialog = new MyDialogFragment();
		editNameDialog.show(fm, "fragment_edit_name");
	}

	@Override
	public void onFinishEditDialog(String inputText) {
		txt.setText("Input value from DialogFragment " + inputText);
	}
}

Please leave your valuable comments if you found this post useful.

You can download the complete source code for the above post from here.

One thought on “How to get a return value from a DialogFragment? or Using DialogFragments in android.

Leave a Reply

Your email address will not be published. Required fields are marked *