Tutorials Android Android Input Dialog Example

Android Input Dialog Example

In this tutorial we show you how to create input dialog in android with code sample.

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. The Dialog class is the base class for dialogs.

Creating a input dialog in android involves below steps

  1. Create layout for input dialog
  2. Inflating layout and initialize AlertDialog

Creating input dialog layout

Here in this example we are creating an simple layout with an TextView and EditText. You may like to add your own layout with different UI widgets you need with your custom styles.

Activity layout (activity_main.xml)

Here we are creating an sample layout for activity. This has just an button, clicking on the button we will show the input dialog.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Input Dialog" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Input dialog layout (input_dialog.xml)

Input dialog layout is composed of a TextView to hint user what to enter, and a EditText to enter values.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Enter Name"
        android:id="@+id/textView" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text here.."
        android:padding="10dp" />
</LinearLayout>

Activity java class

Here in this class we are instating the activity_main.xml layout. A click event is given for the Button, when clicked it shows the input dialog.

package com.javatechig;

import android.app.Activity;
import android.app.AlertDialog;
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;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button button;
	private TextView resultText;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// components from main.xml
		button = (Button) findViewById(R.id.button);
		resultText = (TextView) findViewById(R.id.result);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				showInputDialog();
			}
		});
	}

	protected void showInputDialog() {

		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
		View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
		alertDialogBuilder.setView(promptView);

		final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
		// setup a dialog window
		alertDialogBuilder.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						resultText.setText("Hello, " + editText.getText());
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});

		// create an alert dialog
		AlertDialog alert = alertDialogBuilder.create();
		alert.show();
	}
}

Output

Input Dialog in Android

Total
12
Shares
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
ashish
ashish
8 years ago

How do I show popup on app startup at very first time ?
and want to send popup text to email to some email id? kindly reply

Karthik
Karthik
7 years ago

Both input and dialog xml are same. Provide exact correct code… you have done something and pasted something

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago
Reply to  Karthik

Fixed the layout issue with proper code. Thanks for pointing out.

Bacs
Bacs
6 years ago

Thanks a lot!!

Diego R FG
Diego R FG
6 years ago

How to prevent the dialog from closing? if(value == null) { return; }

sangam mallad
sangam mallad
3 years ago

Nice .it’s working

Previous Post
Android Tutorials

Android CheckBox Example

Next Post

Creating HelloWorld Program in Xamarin.Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
6
0
Would love your thoughts, please comment.x
()
x