sets the ContentView of the dialog to your custom layout.
Closes the dialog
The dialog in the first example(Dialog) does not need to call show() when it is created as it is handled in the constructor
Alert Dialogs must be constructed through a new instance of the AlertDialog.Builder() class. Following the Builder Pattern, all members of the AlertDialog.Builder can be method chained to 'build up' the dialog instance.
The Alert Dialog builder can directly show() the dialog -- you do not need to call create() then show() on the AlertDialog instance
A Basic Alert Dialog
Now this code will achieve this:
(Image source: WikiHow)
Adding Material Design AlertDialog to your app using Appcompat
AlertDialog is a subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method.
The AlertDialog from android.app package displays differently on different Android OS Versions.
The Android V7 Appcompat library provides an AlertDialog implementation which will display with Material Design on all supported Android OS versions, as shown below:
First you need to add the V7 Appcompat library to your project. you can do this in the app level build.gradle file:
Be sure to import the correct class:
Then Create AlertDialog like this:
Alert Dialog with Multi-line Title
The setCustomTitle() method of AlertDialog.Builder lets you specify an arbitrary view to be used for the dialog title. One common use for this method is to build an alert dialog that has a long title.
Custom Alert Dialog with EditText
Xml file: res/layout/alert_dialog.xml
Date Picker within DialogFragment
xml of the Dialog:
Activity calling the dialog:
DatePicker allows user to pick date.
When we create new instance of DatePicker, we can set initial date. If we don't set initial date, current date will be set by default.
We can show DatePicker to user by using DatePickerDialog or by creating our own layout with DatePicker widget.
Also we can limit range of date, which user can pick.
By setting minimum date in milliseconds
By setting maximum date in milliseconds
To receive information, about which date was picked by user, we have to use Listener.
If we are using DatePickerDialog, we can set OnDateSetListener in constructor when we are creating new instance of DatePickerDialog:
Sample use of DatePickerDialog
Otherwise, if we are creating our own layout with DatePicker widget, we also have to create our own listener as it was shown in other example
DatePickerDialog is the simplest way to use DatePicker, because you can show dialog anywhere in your app. You don't have to implement your own layout with DatePicker widget.
How to show dialog:
You can get DataPicker widget from dialog above, to get access to more functions, and for example set minimum date in milliseconds:
Fullscreen Custom Dialog with no background and no title
in styles.xml add your custom style:
Create your custom layout for the dialog:
Then in java file you can use it for an Activity or Dialog etc:
ListView in AlertDialog
We can always use ListView or RecyclerView for selection from list of items, but if we have small amount of choices and among those choices we want user to select one, we can use AlertDialog.Builder setAdapter.
Perhaps, if we don't need any particular ListView, we can use a basic way:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0