Android Snippet – Prompt User Input with an AlertDialog

 Android Snippet – Prompt User Input with an AlertDialog

In this android snippet tutorial, we are going to learn how to prompt user input with an AlertDialog. 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 – CUSTOM DIALOG AND CUSTOM ALERTDIALOG BOX 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 MainActivity.java file.

package com.inducesmile.alertdialoginput.alertdialoginput;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button dialogButton = (Button)findViewById(R.id.button_dialog);
        dialogButton.setOnClickListener(new View.OnClickListener(){

            @Override
            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.setView(userInput);
                inputAlert.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String userInputValue = userInput.getText().toString();
                    }
                });
                inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog = inputAlert.create();
                alertDialog.show();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    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 == R.id.action_settings) {
            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

Add a Comment