Alert Dialog And Custom Dialog For Android
A Dialog is a small window that prompts the user to a decision or enter additional information.
Some times in our application, if you wanted to ask the user about deciding between yes or no in response to any particular action taken by the user, by remaining in the same activity and without changing the screen, you can use Alert Dialog.
Objectives of this Tutorial -
1 Creating a simple Alert Dialog using AlertDialog.Builder.
2 In this example, we will devolve Alert Dialog and Custom Dialog.
How to create Alert Dialog
To create an alert dialog, make an object of AlertDialogBuilder which is an inner class of AlertDialog.
The following syntax is used to create alert dialog: 1. AlertDialog.Builder(Context context) Create a builder for an alert dialog that uses the default alert dialog theme. Syntax: For example: 2. AlertDialog.Builder(Context context, int themeResId) Create a builder for an alert dialog that uses an explicit theme resource. Syntax: For example:
Important method and description of Alert Dialog
1. setIcon(Drawable icon) This method is used to set the icon of the alert dialog box. 2. setCancelable(boolean cancelable) This method is used to set the property that the dialog can be canceled or not. 3. setMessage(CharSequence message) This method sets the message to be displayed in the alert dialog. 4. setTitle(CharSequence title) This method used to set the title to appear in the dialog. 5. setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) This method is used to set a listener to be invoked when the negative button of the dialog is pressed. 6. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) This method is used to set a listener to be invoked when the positive button of the dialog is pressed. 7. show() This method is used to create an AlertDialog with the arguments supplied to this builder and immediately displays the dialog. 8. setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) This method is used to set the callback that will be called when the dialog is dismissed for any reason. For more information visit: https://developer.android.com/reference/android/app/AlertDialog.Builder.html
Example of how to create alert dialog
How to create a custom dialog with custom layout
To create a custom dialog, make an object of Dialog which an inner class of Dialog The following syntax is used to create custom dialog 1. Dialog (Context context); Create an object for a custom dialog that uses the default dialog theme. Syntax: For example: 2. Dialog (Context context, int themeResId) Create an object for a custom dialog that uses an explicit theme resource. Syntax: For example:
Example of how to create a custom dialog
Now let’s see this in action by creating a new project. 1. Create a new project in Android Studio from File > New Project. When it prompts you to select the Empty activity, and proceed 2. Create layout like below in activity_main.xml like this 3. Create a layout resource file for custom Dialog 4. Now create an Alert Dialog using Alert.Builder like this Use setTitle() to set title of Alert Dialog Use setMessage() to set message to Alert dialog Use setPositiveButton() to set positive button of the dialog Use setNegativeButton () to set negative button of the dialog Use show() method to show Alert Dialog Create an alert dialog using below code: 5. Now create a custom dialog like this Use setContentView() method to set custom layout to a custom dialog Now programmatically set height and width of custom dialog using Window like this Use show() method show custom Dialog To access the controls of custom dialog bind all controls like this: Now create a custom dialog like this
Full code of main activity
By using this tutorial now, you can create Alert Dialog and Custom Dialog. You can use modify as per your requirement dialog.
Top-5 Best Mobile Application Development Platform in 2022
It is not an exaggeration to say that Android is the most popular mobile OS with 85% market share and north of 5 million apps in the Google...
What are Android Emulators and How to Setup an Android Emulator?
There are a variety of reasons why you would want to run an Android emulator on your PC. Mobile app developers always look to test their app...