Andy Dennie
Andy Dennie

Reputation: 6052

How to center a dialog within a fragment?

In a scenario where I've got multiple fragments on display (e.g. list fragment and detail fragment), and one of the fragments is showing an alert dialog (a DialogFrament), I'd like the dialog to be centered within the fragment that is showing it, not centered within the whole screen. Is there a way to do that without a whole lot of pixel calculations involving the dimensions of the screen, the dimensions and position of the fragment, etc.?

Upvotes: 2

Views: 1677

Answers (1)

gkee
gkee

Reputation: 771

You can do this by using a FrameLayout, as Joe Simpson mentions. Say you want to put your dialog fragment over the top of the Details fragment that you mention. Put a FrameLayout around the details fragment in your activity layout, then embed the dialog fragment within the FrameLayout too (with layout_gravity="center"). Set the visibility="gone" until you need it. E.g.:

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <fragment
                    android:id="@+id/my_details_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    class="com.my.fragment.DetailsFragment" />

                <fragment
                    android:id="@+id/my_dialog_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    class="com.my.fragment.DialogFragment"
                    android:layout_gravity="center" />
            </FrameLayout>

You can also start it programmatically. Put a holder view (e.g. a LinearLayout) in the xml:

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <fragment
                    android:id="@+id/my_details_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    class="com.my.fragment.DetailsFragment" />

                <LinearLayout
                    android:id="@+id/myfragment"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" />
            </FrameLayout>

... and then replace it in code like so:

            final MyDialogFragment dialog = MyDialogFragment.getInstance();

            // get an instance of FragmentTransaction from your Activity
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                    .beginTransaction();
            fragmentTransaction.add(R.id.myfragment, dialog);
            fragmentTransaction.commit();

Note that the docs on DialogFragment say it's fine to embed it like this: "A DialogFragment can still optionally be used as a normal fragment, if desired. This is useful if you have a fragment that in some cases should be shown as a dialog and others embedded in a larger UI."

Upvotes: 3

Related Questions