Search code examples
androiddialogfragment

dialogframgent width


I am using dialogFragment to make my own custom fragment but its size does not look like what is in priview
this is what is showing in preview

enter image description here

and this what happens when the dialog is shown

enter image description here

I want it to look exactly like my preview - what should I do?


Solution

  • Try this

        final Dialog dialog = new Dialog(mContext, R.style.Theme_Transparent);
    
        View view = LayoutInflater.from(mContext)
                               .inflate(R.layout.layout_custom_dialog, null);
        dialog.setContentView(view);
    
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = wlp.MATCH_PARENT;
        wlp.gravity = Gravity.TOP;
        wlp.flags &= ~WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
        window.setAttributes(wlp);
    

    And add this to styles.xml file

        <style name="Theme.Transparent" parent="android:Theme">
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:backgroundDimEnabled">true</item>
        </style>