Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an overlay view within an activity

Tags:

android

I have a requirement where i have an activity which shows list of items like facebook feeds and when clicking on a button from one of the list item a dialog has to popup which will show comments for that item.

I was going through the documentation and found out that we have to create a DialogFragment on the fly to achieve this. Please advice if this is the right approach.

enter image description here

enter image description here

like image 251
Kapil Raju Avatar asked Oct 13 '13 05:10

Kapil Raju


1 Answers

You don't actually have to use a Dialog. I think dialogs are more appropriate when you want to show simple views or just an alert/confirmation to the user (normally done with an AlertDialog).

For your situation I guess the best approach would be to have a FrameLayout on your Activity, sibling of your main layout element, and add a Fragment to it when you want to show a popup like that over your main Activity's layout. As long as you put the fragment's view after your activity's root layout element, the fragment will be displayed on top of your main layout, as an overlay. e.g.:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">
        <!-- Activity's main layout here -->
    </LinearLayout>

    <FrameLayout android:id="@+id/overlay_fragment_container"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>
</merge>

and then on your Activity when you want to display the fragment you do:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
        .add(R.id.overlay_fragment_container, yourFragment)
        .commit();

Hope it helps :) Luck!

like image 137
Victor Elias Avatar answered Nov 13 '22 00:11

Victor Elias