How to set Full screen dialog with dialog fragment
Asked Answered
R

7

10

I am making full screen dialog with dialog fragment. I am successfully shown dialog but problem is in its width and height. It cannot shows full screen size. I want to set width and height with accumulating full screen size like an Activity. Kindly guide me how to set dialog width and height to accumulate it to full screen size.

CustomeDialogFramgnet:

public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment's layout, regardless
     of whether it's being displayed as a dialog or an embedded fragment. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        View rootView = inflater.inflate(R.layout.item_fragment, null, false);

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setTitle("Dialog title");

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
        }

        return rootView;
    }

    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        getActivity().getMenuInflater().inflate(R.menu.alert_dialog_input, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_save) {
            // handle confirmation button click here
            return true;
        } else if (id == android.R.id.home) {
            // handle close button click here
            dismiss();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

item_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"
            android:text="zohaib">
        </TextView>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>
Rebatement answered 30/8, 2016 at 6:24 Comment(4)
you can use bottombar in dialog fragment, after all you are using coordinator layout.. you can drag as a full screenRowe
Please write some sample codeRebatement
refer this one.. it might help you... androidhive.info/2016/04/…Rowe
Small offtopic detail - there's missing setHasOptionsMenu(true) in onCreateView. Without it the menu will not render and of course onOptionsItemSelected will never be called.Kellikellia
T
21

Create a theme in your style.xml which extends from Theme.AppCompat.Light.DarkActionBar, something like below:

<style name="DialogFragmentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowNoTitle">true</item>
</style>

and set the style you created while opening the dialog, something like below:

CustomDialogFragment mDialog = new CustomDialogFragment();
...
mDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);
mDialog.show(getSupportFragmentManager(), "DialogFragment");
Touched answered 30/8, 2016 at 6:48 Comment(5)
Great, have a nice day.Touched
One more problem, dimiss() function also does not work. have you any idea?Rebatement
it should work ideally, can you post the code where are you calling it?Touched
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_save) { // handle confirmation button click here return true; } else if (id == android.R.id.home) { // handle close button click here dismiss(); return true; } return super.onOptionsItemSelected(item); }Rebatement
here you are trying to set the toolbar view of your fragment to actionbar of your activity, I don't think it works that way, and Your menu click for android.R.id.home must not be called because you are not clicking the home button of activity. Instead you can simply keep that home button as a view inside toolbar of your fragment, and handle it with onclick listener.Touched
H
5

set this line after creating dialog.

dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

If you are creating custom DialogFragment write this in onCreate of your DialogFragment:

setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

reference

Heatherheatherly answered 30/8, 2016 at 6:25 Comment(2)
i added after this line Dialog dialog = super.onCreateDialog(savedInstanceState); but not workRebatement
any other method you've?Rebatement
S
2

Create theme like this and add to your dialog

<style name="ThemeDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>

set this line after creating dialog

getDialog().getWindow()
            .getAttributes().windowAnimations = R.style.ThemeDialog;
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.gravity = Gravity.FILL_HORIZONTAL;
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Supersede answered 30/8, 2016 at 7:11 Comment(1)
Setting gravity to FILL_HORIZONTAL is the thing I was looking for. Thanks.Sinistrorse
D
1

Create below theme in your style.xml:

<style name="DialogTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowNoTitle">true</item>
</style>

then set the style in DialogFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
Dysgenics answered 2/12, 2017 at 9:4 Comment(1)
most appropriate and clean solutionCrossed
S
0

Found a safer way and it seems conventional to Android. DialogFragment does inherit from Fragment. So, just show the fragment like any other:

activity.supportFragmentManager
        .beginTransaction()
        .replace(R.id.contentFragment, dialogFragment)
        .addToBackStack(null)
        .commitAllowingStateLoss()
Sesterce answered 1/10, 2020 at 20:10 Comment(0)
S
0

try this

dialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

if the above not working then add one more thing inside onCreate of your dialog fragment

setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);
Sprinkling answered 21/10, 2021 at 7:42 Comment(0)
D
-1

Add this line in onCreate where you are inflating your layout.

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Diffract answered 15/8, 2017 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.