Show Alert Dialog in Android
AlertDialogs are pop ups used to prompt a user about an action to be taken. An AlertDialog may also be used for other actions such as providing a list of options to choose from or can be customized to have a user provide unique details such as their login information or application settings.
Contents
Steps
Understanding AlertDialogs
- Understand the basic structure of an AlertDialog. An AlertDialog is when the Android app uses the Android system to put up important information for a user to read.
- A title is optional but can be useful to put in a simple message or question. This can also contain an icon
- The content area which can display a message, list or other custom layout functions.
- Action buttons that are used for the user to send a response to the AlertDialog box. Can be a positive button, a negative button or a neutral button. You can only have one of each type and up to three buttons on a given alert dialog.
An AlertDialog can also be used to warn or ask the user to change an important setting. It will contain three components of the dialog box.
- Understand what a class is. A class is a template that allows you to create other objects that have their own properties and behaviors. The AlertDialog class is a subclass of the Dialog class that features its own unique properties of being able to display up to three buttons in addition to a standard Dialog prompt.
- Decide on the purpose of the AlertDialog. What is the purpose of the AlertDialog box? What are the options going to be for the user? Is it possible that the user would ignore this process otherwise? Write down what the user will be prompted for and note their choices and what they do. If the writing seems unclear for the user, they may not understand the purpose of the AlertDialog box.
- Write and illustrate the AlertDialog. Draw what you want the AlertDialog box to look like. Write down the list the options you would like to implement and their resulting actions. Think carefully about what the user is being prompted for and ensure the writing does not create ambiguity.
- Download and Install Android SDK. A software development kit or SDK is used to develop software in a specialized environment for creating programs and applications. You can download SDK direct from the Android developer website.
- Create a new project. Even if you have an existing project, creating a project can be ideal if you want to create a testing environment before adding code to your main project. From the menu bar click on File>New>New Project… and follow the prompts to create a new application.
Coding an AlertDialog Prompt
- Create a trigger for the AlertDialog box. The AlertDialog will need to be brought up by an action performed by the user. You can edit the main layout of the app under the “activity_main.xml” file to provide a button to test the AlertDialog with. There are two ways to create a button. Switch between design and coding methods by clicking on either Design or Text which are located at the bottom of the main window pane to switch between the different modes.
- The Android SDK allows you to edit your layout such as the main front end which can be found in the project hierarchy under the Layout folder by using a drag and drop interface to create a button.
- You can also create a button in XML code yourself by editing the XML document. Note the line indicating the onClick action is used to execute the AlertDialog box when the button is clicked on. Template:CodeBox
- This class will be useful for your entire application. Be sure to add it to the top of your code hierarchy.
- Create the alert dialog box object and set the builder to refer to the new object and create the box.
Coding a List AlertDialog
- Create an array. You can use one of three different types of lists you can use. Instead of using the setMessage function, use a list if you want to provide multiple choice answers. Your list will need to have an array created independently to list the different available options. The list will use an array to display the different available options.
- Create a list AlertDialog. Use setItems to provide a list of options to choose from. This will appear as a list of radio buttons to check. The function will require an array of options to choose from and an onClickListener to represent the user’s input.
- Create a list that features multiple choice. Use setMultiChoiceItems if you want the user to be able to select several options. Their options will be shown in checkboxes when used.
- Create a list that only permits a persistent single choice. Use setSingleChoiceItems if you want the user’s singular choice to be persistent. Their options appear with radio buttons which appear as circles with dots inside of a selected choice.
Creating a Custom AlertDialog
- Create a custom layout. A custom AlertDialog allows you to create a layout featuring it’s own parameters and can obtain information that can be used to obtain a user’s login information, configuration settings and more. You can create a new layout that will be created in XML coding format. Some Android SDK provide the ability to use a drag-and-drop function to easily create a layout that will auto-convert into XML for you. From the menu bar at the top of the window, click on File>New>XML>Layout XML File. Provide a layout file name then click on Finish. Your new layout will appear in the main window pane.
- Add in widgets or other components to the layout. You can add in components using one of two methods. You can open the layout file from looking in the project hierarchy shown on the left hand side. Then open the following folder paths: “<AppName>>app>src>main>res>layout”
- Create a new Java Class. A new class will allow you to separate the code for your unique alert layout. Click on File>New>Java Class. Type in a name for your new Java class then click on OK. For this example, we will call this example class “CustomDialogExample.”
- Import the Dialog Fragment. The Dialog Fragment allows for maximum compatibility with the different versions of Android OS.
import android.support.v4.app.DialogFragment;
- Make sure the main class method extends to the DialogFragment.
- Create a layout inflater object and a view object. A Layout Inflater instantiates a layout XML file into view objects. A View object is the basic structure for user interface components in a rectangular screen space and draws objects and widgets on screen.
- Create the custom dialog layout. We will need for it to be public so that it can be accessed elsewhere in the application and it will return a Dialog object. It will take in a Bundle object
- Inflate the layout from the custom XML layout. With the LayoutInflater and View objects created, inflate the layout and obtain the custom layout onto the View object from within the onCreateDialog function.
- Build the custom AlertDialog. In the onCreateDialog function, use the AlertDialog builder to create the layout.
- You may wish to add in a button to close the AlertDialog.
Tips
- The SDK will note if something cannot be invoked indicated by red text. Clicking on the text will have the SDK prompt you to import the corresponding library to add to your project. Hold Alt+↵ Enter to add the library to your project.
Sources and Citation
- http://developer.android.com/reference/android/app/AlertDialog.html
- http://www.edureka.co/blog/android-tutorial-on-alertdialog/
- http://developer.android.com/sdk/index.html
- http://developer.android.com/reference/android/widget/Button.html
- http://developer.android.com/reference/android/app/Dialog.html
- http://developer.android.com/reference/android/view/LayoutInflater.html
- http://developer.android.com/reference/android/view/View.html