Android prompt user input dialog example

In this tutorial, we will enchance the previous AlertDialog example, to make it able to accept user input, just like a PromptDialog. More specific, this is a custom AlertDialog example.

See following steps :

  1. Create a prompt dialog layout (XML file).
  2. Attach the prompt dialog layout to AlertDialog.Builder.
  3. Attach the AlertDialog.Builder to AlertDialog.
  4. Done.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

You may interest to read this custom dialog example.

1 Android Layout Files

Two XML files, one for main screen, one for prompt dialog.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="Show Prompt Dialog" />
        android:layout_height="wrap_content" >

File : res/layout/prompts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:padding="10dp" >
        android:text="Type Your Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
        android:layout_height="wrap_content" >
        <requestFocus />

2. Activity

Read the comment and demo in next step, it should be self-explorary.

File :

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 MainActivity extends Activity {
	final Context context = this;
	private Button button;
	private EditText result;
	public void onCreate(Bundle savedInstanceState) {
		// components from main.xml
		button = (Button) findViewById(;
		result = (EditText) findViewById(;
		// add button listener
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// get prompts.xml view
				LayoutInflater li = LayoutInflater.from(context);
				View promptsView = li.inflate(R.layout.prompts, null);
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
				// set prompts.xml to alertdialog builder
				final EditText userInput = (EditText) promptsView
				// set dialog message
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
						// get user input and set it to result
						// edit text
					  new DialogInterface.OnClickListener() {
					    public void onClick(DialogInterface dialog,int id) {
				// create alert dialog
				AlertDialog alertDialog = alertDialogBuilder.create();
				// show it;

3. Demo

Start it, the “main.xml” layout is display a button and edittext (result).

android prompt user input example

Click on the button, display a prompt dialog “prompts.xml” layout, type message “mkyong“, and click on the “OK” button.

android prompt user input example

User input “mkyong” will pass to the “main.xml” layout, edittext (result), and display it.

android prompt user input example

Download Source Code

Download it – (16 KB)


  1. Android AlertDialog Javadoc
  2. Android Dialog example
  3. Android custom dialog example
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: pay day loans()

  • Pingback: stop parking()

  • Pingback: her og nu laan()

  • Pingback: car parking()

  • Pingback: water ionizer()

  • Pingback: laan penge billigt()

  • Pingback: mobile porn movies()

  • Pingback: tv packages()

  • Pingback: best online casinos()

  • Pingback: How to unblock Viber in Dubai UAE()

  • Pingback: enagic kangen water()

  • Pingback: kangen water()

  • Pingback: water purification systems()

  • Pingback: water ionizer()

  • Pingback: watch tv show episodes()

  • Pingback: watch tv show episodes()

  • Pingback: tv online, online tv()

  • Pingback: how to increase leptin()

  • Pingback: Blue Coaster33()

  • Bharat Patidar

    Thank you very much for help, You make my day.

  • R2

    NICE share bro, your article help me to solve problem i got when handling pop up with input dialog :D

  • K.Dam

    Thanks for the articles. I’ve found a lot of interesting and useful things about android here!

  • faeze_el

    great tutorial,like the previous ones.
    thanks a lot.

  • Dode

    Thank you sir, this post help me alot :D

  • Hardik

    Thank you very much for all of these awesome tutorials… They have helped me very much.. Keep up this awesome work..

  • Max Tomlinson

    Great demo – thanks for posting

  • kingdom and dragons hacks

    Hey there would you mind letting me know
    which web host you’re working with? I’ve loaded your blog in 3 different browsers and I must say
    this blog loads a lot faster then most. Can you recommend a
    good hosting provider at a fair price? Many thanks, I appreciate it!

  • nel

    Thank you very much.. Simple and understandable.. Suits for all beginners

  • Pingback: How can i add dialog box on click of a button in android? : Android Community - For Application Development()

  • Azmi

    Can you please explain how can i get java to read an XML file, also how can i make the program to prompt user for files from XML and display them after?? I would be really thankful if you/anyone could help me??

  • Droidster

    FINALLY! Unlike so many sites with assuemed knowledge tutorials, this one has it down for anyone to try! THANK YOU!!

  • Shalgham

    really thanks it’s helped

  • vraa

    Thanks, reading values from inflated custom layout can be a bit tricky..if you try to use findViewById without referencing to the inflated view.

  • Nizzy

    I keep coming here to copy codes for my Android project. Today, I just want to say Thank You.

  • http://[email protected] Pritam

    Very great tutorial … can u plz give the example of custom dialog with “textview” and
    “edittext” and a “Button” something should be happen when udser clicks on the button

  • Pingback: Aiuto per Dialog - Forum Android Italiano()