Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show fragment as a dialog?

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

like image 385
Jayesh Avatar asked May 29 '13 11:05

Jayesh


People also ask

Is dialog a fragment?

A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I show custom dialog?

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.

What is dialogue fragment and explain its uses?

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.


1 Answers

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.

like image 78
Anish Mittal Avatar answered Nov 16 '22 02:11

Anish Mittal