How to Prompt User Input with an AlertDialog in Android

In this android snippet tutorial, we are going to learn how to prompt user input with an AlertDialog in android.

When developing android application, there are situations that can necessitate using AlertDialog to get or prompt user input.

If you are looking for a detail tutorial on android custom AlertDialog, I will suggest you read one of our tutorials – How to Create Custom Dialog and AlertDialog in Android.

Before we start, the first thing I will do is to list the environment and tools I used in this android snippet tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 8

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AlertDialogInput

Package: com.inducesmile.alertdialoginput

Keep other default selections.

Continue to click on next button until Finish button is active

Then click on Finish Button

Once you are done, copy and paste the code snippet below in your file.

package com.inducesmile.alertdialoginput.alertdialoginput;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {

    private Context context = this;

    protected void onCreate(Bundle savedInstanceState) {
        Button dialogButton = (Button)findViewById(;
        dialogButton.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v) {
                final AlertDialog.Builder inputAlert = new AlertDialog.Builder(context);
                inputAlert.setTitle("Title of the Input Box");
                inputAlert.setMessage("We need your name to proceed");
                final EditText userInput = new EditText(context);
                inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String userInputValue = userInput.getText().toString();
                inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                AlertDialog alertDialog = inputAlert.create();
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == {
            return true;
        return super.onOptionsItemSelected(item);

Save the file and run your project. If everything works fine you will see an interface like the one shown below in your test device or emulator.

Prompt User Input with an AlertDialog

You can download all the codes for this tutorial below. If you are having hard time downloading the tutorials, you can drop your email address and I will forward it to your inbox.


Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.


One Response

  1. Avatar

Add a Comment