What is alertdialog in android and in what cases can it be used?
10 minute(s) read | Published on: May 11, 2021 Updated on: Dec 14, 2021 |
AlertDialogs
have a text that informs the user and contains several buttons that are performed according to the desired operation and the text in which it is written. AlertDialogs can be used to display warnings, delete notifications, inform users. AlertDialogs can hold up to three buttons, and users can perform the desired operation according to the text inserted in it. If you want to know about this and create a project or work with AlertDialog, we suggest following this tutorial.
builder example
1- Create a new project in Android Studio called AlertDialog (alert dialog android studio). Of course, it should be noted that the project's name is arbitrary, and you can choose another name for it. The chosen name for this project in this tutorial is AlertDialog.
2- After creating the project, it is better to create an Empty Activity.
3- In this project, we need the appcompat-v7 library to perform our desired operations. It is better to add it to the project as well.
4- In the activity, we create an example of this, and of course, we choose the desired name for it. For example, the name of choice in this project is alertBuilder.
5- We have to do the following inside the onCreate method.
In the code above, AlertDialog.Builder currently receives a parameter, which is also Context.
Note:
Be sure to select AlertDialog, which is related to the support library.
6- Create a simple AlertDialog.
7- Then, we complete it as follows.
alert dialog example
In the code above, the setTitle and setMessage methods specify the title and text of the message, respectively. In other words, these methods are used to specify the title and text of the message.
We can use the setNegativeButton, setPositiveButton, and setNeutralButton methods to place buttons in the AlertDialog.
In the codes listed above, we have used only one parameter and method; So, the message that will be shown to the user has a button. That button will also be displayed as Yes.
We knew that entering the above code would create an AlertDialog that would show the text to the user and that the text would also have a Yes button.
Then at the end of the code, we used the create () and show () methods to create and display the AlertDialog.
8- We run the project to see the result.
9- After running the project, we will see that all the changes and settings that we have applied for it have been successful, and its output displays an AlertDialog with a button.
10- Define a Listener for the button we created.
11- Replace a Listener of DialogInterface.OnClickListener ().
We knew that if we define commands inside onClick, it will be executed by touching the button.
12- Add a Toast message to the Listener.
13- We will implement the project.
14- After executing the project, we will see that the corresponding message will be executed by touching the Yes button.
15- Then, we add other methods to the activity as below.
16- Then, we run the project.
17- After running the project, we see that three buttons have been added to the AlertDialog.
18- The button related to the Neutral Button is located on the left.
19- To add an icon to the AlertDialog, we can use setIcon.
The code above to add an icon defines an icon called logo.png located in the project's drawable directory.
20- It should be noted that if the AlertDialog window is open and the user touches somewhere outside this window, the AlertDialog will be closed. Assume that the AlertDialog is open and the user touches another screen outside the window instead of clicking or touching the buttons in this section, in which case the AlertDialog window will close. This is a problem that we need to solve.
alertdialog error
21- We use the setCancelable method to fix this alert dialog error problem and set its value to false.
By entering the above codes, we cause that if the user clicks or touches somewhere other than this window, the AlertDialog window will not be closed and will remain open.
22- The complete codes that should be included in the AlertDialog section are as follows.
23- If we want to write the code concisely that reduces the volume of code in the project, we can change and complete them as follows.
24- Add a Button to the activity.
25- Then, we have to define all the codes related to AlertDialog inside a separate function.
26- The codes that should be in the activity_main.xml section are as follows:
27- The complete and final codes that should be in the MainActivity.java section are as follows:
28- It should be noted that another feature and capability of AlertDialog is to show a list of items for the user, and the user can choose from the items that are displayed as a list.
Therefore, we must apply the settings so that we can display the list using AlertDialog, and that list also contains options, and the user can select an item from among them.
If we want to say it so that you understand better, we will give an example that the user can use this AlertDialog feature to determine the level of difficulty and ease of the game steps.
This means that when the user wants to select the difficulty or ease of the game, this AlertDialog feature is used to easily choose the difficulty or ease.
29- In the activity, we create an instance of String [].
30- Then, we define some values for it.
31- Then, we display the defined values using setSingleChoiceItems. Like the following:
The method in the code listed above has three parameters and input, the first input of which is items, i.e., the values we have specified for the list items.
In the second parameter of this method, we can specify which of the values and items in the list are selected by default. In this section and the codes listed above, we entered a value of 2 for this parameter, which means that the count starts from element 0, and if we want no option to be displayed by default, we must enter a value of -1 for it. Determine.
The third parameter of this method is the Listener, which we have nothing to do with, and we set its value to null.
Note:
You must note that the setMessage and setSingleChoiceItems parameters can never be used simultaneously.
32- The complete code that should be in the MainActivity.java section is as follows:
33- Then, we have to run the project to see the results.
34- It is also possible to define Listener for the setSingleChoiceItems method.
35- To do this, we define an example of a String with an arbitrary name in the activity.
36- Then, we have to complete the Listener as follows:
By inserting the above code into an activity, we cause the user to print the name of each item he selects from the lists inside a Toast.
37- It should be noted that we can even style the AlertDialog.
38- Add a style to styles.xml and choose its name as desired. The name chosen in this tutorial is DialogTheme.
39- The new style we add inherits from Theme.AppCompat. Light. Dialog. Alert. alertdialog button color
40- It should be noted that to connect the style we define to the dialog so that its styles are connected to the dialog and make changes, we must specify it as the second input parameter of the AlertDialog.Builder.
If I set the style as the second input parameter of AlertDialog.Builder, we can apply the defined styles to the dialog.
41- If we want to define a border or radius for the dialog window, I must create a resource file inside the drawable directory.
42- The codes that should be in the dialog_background.xml section are as follows:
In the code above, inset Left and the other three can specify the distance of the dialog box from the screen.
In the code above, we have a shape tag in which we set a value for the curvature of the dialog corners, the background color of the dialog, the thickness of the border bar, and its color.
43- Replace the background property with the code below in the DialogTheme style.
44- It should be noted that we can also determine the color of the buttons separately.
45- To do this, we add a few buttons to it and apply changes to it to change the color of the buttons. Like the following:
46- Then, we have to define separate styles for each of the buttons and add them to the DialogTheme below.
47- If we run the project, we will see that each button has color and style.
48- Another feature and capability of AlertDialog can be called Custom Layout.
49- Using these features and capabilities, I can apply changes and settings so that, for example, in the dialog window, the Username and Password fields are added so that the user can use these ways to access his account in entering the application.
50- To do this, we add a layout to the project.
51- The codes that should be in the custom_dialog.xml section are as follows:
52- Use LayoutInflater to inflate the top layer and add it to AlertDialog using the setView () method.
In this part of the tutorial, you will learn what AlertDialog is and its use. If you follow all the above steps with high accuracy in this tutorial, you can easily create an AlertDialog.
In short, AlertDialog is the items displayed to users in the list, and users can choose one of the items displayed in the list. AlertDialogs have many uses, including choosing the difficulty level or ease of play. Games use AlertDialog to choose the level and difficulty of the game to their liking.
Another use and feature of AlertDialogs is showing text to users for informational purposes. In this case of the AlertDialog feature, several buttons can be defined in the dialog window to be displayed, and users can select the desired button according to the text displayed in the dialog window.
You can even select an icon for AlertDialog, and you can select and display the appropriate icon depending on the message type.
If you follow all the steps mentioned in this tutorial carefully, you can create a project that can launch it.
About our Android and iOS app development services