I would like to create a home screen with a login button, which when clicked, opens a dialog box to enter a password and then either submit or cancelled.
I have read that there are two methods to do this in order to create a DialogFragment
- using onCreateDialog()
and onCreateView()
.
What is the difference between these two options, and which one is used in which scenarios?
To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .
Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.
The onCreateDialog()
is for displaying Basic Dialog. This is the simplest way to display a dialog. While using this method you need to use the builder method of the inbuilt dialog like AlertDialog to build the dialog and listeners for positive and negative button and return a Dialog.
On the other hand OnCreateView()
you can return a view to be used as a dialog. You can use your own layout to build a dialog UI. Your own button and listen for them. In simple words your own Custom Dialog.
Also Note: You can even set a view to your built in Dialog like AlerDialog in onCreateDialog()
by calling setView() method. The onCreateView()
is used to build your dialog from your own layout. Which version should you use is entirely upto you. I hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With