How to create an overlay view within an action

I have a requirement when I have an activity that displays a list of items, such as facebook feeds, and when I click on a button from one of the list items, a dialog box opens in which comments for this item will be displayed.

I went through the documentation and found out that we need to create a DialogFragment to achieve this. Please advice if this is the right approach.

enter image description here

enter image description here

+6
source share
1 answer

You really don't need to use dialogue. I think that dialogs are more appropriate if you want to show simple presentations or just an alert / confirmation to the user (usually this is done using AlertDialog).

In your situation, I think the best approach would be to have a FrameLayout in your activity, a sibling of your main layout and add a snippet to it when you want to show a popup similar to this main activity layout. As long as you place the fragment view after the root activity layout element, the fragment will appear on top of your main layout as an overlay. eg:.

<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 main layout here --> </LinearLayout> <FrameLayout android:id="@+id/overlay_fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </merge> 

and then in your activity, when you want to display the fragment that you are doing:

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

Hope this helps :) Good luck!

+16
source

Source: https://habr.com/ru/post/955858/


All Articles