I have developed application in which I want to display Fragment as a dialog,
I used Tabs and Fragment in my application, I have just one activity and I replace the fragment as I need,
If we used activity then we declare "android:theme="@android:style/Theme.Dialog" in Manifest file to display activity as dialog, same thing I want to do for Fragment
A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
This example demonstrate about how to make custom dialog in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
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.
We can show a Fragment as a dialog using two means , but a single way.
Explanation:
Way:
Extend class DialogFragment and override any one of two methods:
onCreateView() OR
onCreateDialog().
Diff. between these two:
Overriding onCreateView() will let you show a Fragment as dialog and you can make the Title text customized.
On the other hand, overriding onCreateDialog(), you can again show a fragment as dialog and here you can customize the entire dialog fragment. Means, you can inflate any view to show as dialog.
If you need any source code explaining the above text, let me know.
Note:
Using DialogFragment has a disadvantage. It doesn't handle screen orientation. And crashes the app.
So, you need to use setRetainInstance() inside onCreate() of DialogFragment class.
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