An android dialog is a small window that prompts the user to enter some information or make a decision. A dialog does not cover the whole screen; it only covers small part of the screen. Sometime dialog is normally displayed at the center of screen. It can be displayed at the bottom of screen and some time at the top of the screen also. In this tutorial, you will learn to make a user input alert dialog, where user input is something in the dialog box.

AlertDialog component is used to make dialog box in android and you can customize its UI according to your need. Here I have made a custom dialog to get user information with a title by using TextView, user input box using EditText and with two button send and cancel.

Simple Android Alert Dialog
Android Custom Alert Dialog Example
Dialog Libraries for Android Project

Android Example: How to Make Android Prompt User Input Dialog

Here I have created a simple dialog layout with a TextView and EditText. If you want different layouts, you can make your own layout.

XML Layout File

Open your XML layout file and add a button with an id openUserInputDialog. Main XML layout file will look like below.


Now, create a new XML layout file for dialog user interface called user_input_dialog_box and add a TextView and an EditText inside LinearLayout. Following is the complete content of XML layout file.


Java Activity File

Open your app java activity file and add LayoutInflator, AlertDialog Builder and link to the user_input_dialog_box layout that we create above in the button OnClickListener. Following is the complete java code of AndroidUserInputDialog.java file.

src/ AndroidUserInputDialog.java

Android Example: How to Make Android Prompt User Input Dialog

That’s all. Now run your Android User Input Dialog Example application and click on the Open User Input Dialog button, the dialog box will appear with title and user text input. If you click on the Send or Cancel button, the dialog will disable which will look like the above screenshot.

Post a Comment

Powered by Blogger.