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>
Zohaib Siddique
  • 115
  • 1
  • 1
  • 9

7 Answers7

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");
himanshu1496
  • 1,921
  • 19
  • 34
  • Great, have a nice day. – himanshu1496 Aug 30 '16 at 07:32
  • One more problem, dimiss() function also does not work. have you any idea? – Zohaib Siddique Aug 30 '16 at 07:35
  • it should work ideally, can you post the code where are you calling it? – himanshu1496 Aug 30 '16 at 07:46
  • @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); } – Zohaib Siddique Aug 30 '16 at 07:53
  • 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`. – himanshu1496 Aug 30 '16 at 08:08
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

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
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);
Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
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);
}
Zahra.HY
  • 1,684
  • 1
  • 15
  • 25
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()
andude
  • 500
  • 1
  • 5
  • 11
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);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Ashish Choudhary
  • 444
  • 1
  • 4
  • 11
-1

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

getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Hamza Khan
  • 1,433
  • 13
  • 19