Android input dialog

When you create an application that needs to display an input dialog to the user, this Android tip might be useful to you. In this Android tip, I help you to create an input dialog that shows up to allow the user to input text into the text box (EditText). The input text will be passed to the activity before the input dialog is dismissed by pressing the OK button.
Before you can display the input dialog to the user and pass the input text to its parent activity (MainActivity), you create a class that extends the DialogFragment class. In this example, the name of the class is InputDiaglogFragment. In this class, you create an interface (OnResultDialog). In the interface, you define a method called onGetDialogResult that will be implemented in the parent activity. This method is called from the InputDiaglogFragment class (before you call the dismiss() method to close the dialog) to pass the input text to the activity. You should note that to be able to call the onGetDialogResult method that is implemented in the activity, you need to cast the activity to OnResultDialog when the dialog is attached to its parent activity.

package com.example.inputdialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class InputDialogFragment extends DialogFragment{
private OnResultDialog mCallback;
@Override public void onAttach(Activity activity) {
// Cast the parent activity to OnResultDialog so that you are able
// call the onGetDialogResult implemented the activity
mCallback = (OnResultDialog)activity;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set dialog title
getDialog().setTitle("Input dialog box");
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.dialoglayout, container, false);
// Create a reference to the EditText
final EditText txtInput=(EditText)view.findViewById(;
// Add click listener to the button
// Create a reference to the Button
Button bt=(Button)view.findViewById(;
// Add click listener to the button
bt.setOnClickListener(new OnClickListener(){
public void onClick(View view){
// Pass the input text to the parent activity
dismiss(); //close the dialog
return view;

public interface OnResultDialog{
public void onGetDialogResult(Object result);


Here is the layout file (dialoglayout.xml) that is required to display an input text box and a button on the input dialog. This file is placed in the res/layout directory.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
  android:orientation="vertical" >
   android:hint="Enter a value"


To display the input dialog, in the MainActivity, you create an instance of the InputDialogFragment and call the show() method.

package com.example.inputdialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements InputDialogFragment.OnResultDialog {

   protected void onCreate(Bundle savedInstanceState) {
     //create dialog instance
     InputDialogFragment df=new InputDialogFragment();
     //show the dialog, "df");     
   // Implementation of the onGetDialogResult method of the OnResultInterface
   // defined in InputDialogFragment class
   public void onGetDialogResult(Object result){
   // Display the input text from the input dialog in LogCat windows
   Log.i("Main", result.toString());



Posted by: Dara | post date: 12-10-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.