Thursday, August 7, 2014

Use Prompt Dialog in Android

A Prompt Dialog box can be used to prompt the user to enter an input. In this tutorial, we shall enhance the AlertDialog example illustrated previously to enable it to accept user input. This is precisely a custom AlertDialog example.

We shall follow the following steps :
i. Create a prompt dialog layout in XML
ii. Attach the prompt dialog layout to AlertDialog.Builder
iii. Attach the AlertDialog.Builder to AlertDialog

1. XML layout files

We shall use two files- main.xml and custom.xml to display the dialog box, the earlier file shall display the main screen and the latter shall display the prompt dialog.

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical">
    <Button android:id="@+id/buttonPrompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Prompt Dialog" />
<EditText android:id="@+id/editTextResult"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
</EditText>
</LinearLayout>

File : res/layout/prompt.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >
    <TextView android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Your Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
<requestFocus />
</EditText>
</LinearLayout>

2. Java Code

We shall add a listener to the button and set the dialog message.

File: Main.java

package com.endeavour.tutorial;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
       final Context context = this;
       private Button button;
       private EditText result;
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              // components from main.xml
              button = (Button) findViewById(R.id.buttonPrompt);
              result = (EditText) findViewById(R.id.editTextResult);
              // add button listener
              button.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View arg0) {
                           // get prompts.xml view
                           LayoutInflater li = LayoutInflater.from(context);
                           View promptsView = li.inflate(R.layout.prompt, null);
                           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                           // set prompt.xml to alertdialog builder
                           alertDialogBuilder.setView(promptsView);
                           final EditText userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
                           // set dialog message
                           alertDialogBuilder.setCancelable(false)
                                  .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                      public void onClick(DialogInterface dialog,int id) {
                                         // get user input and set it to result
                                         // edit text
                                         result.setText(userInput.getText());
                                      }
                                    })
                                  .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                      public void onClick(DialogInterface dialog,int id) {
                                         dialog.cancel();
                                      }
                                    });
                           // create alert dialog
                           AlertDialog alertDialog = alertDialogBuilder.create();
                           // show it
                           alertDialog.show();
                     }
              });
       }
}

3. Output

The following output shall be displayed.

i. Home screen with the "Show Prompt Dialog" button


ii. The prompt dialog box appears when the button is pressed


iii. Enter some text and press OK


iv. The text field is filled when "OK" button is pressed


No comments:

Post a Comment