How to set the width of a DialogFragment in percentage?
Asked Answered
H

4

13

I have a DialogFragment with an xml layout and I need it to be, let's say 75% as wide as the screen. When I look for answers I always find the common weight-solution, which does not help me, as my fragment overlays the current activity as shown below and does not take the full screen's size. I also don't want to use "ems" as a fix width as I can't know the user's screen size.

enter image description here

Is there a way (preferably in xml) to set the root LinearLayout's width to said 75%? I think it would be a possible workaround to define a whole set of LinearLayouts and make some of them transparent but this seems to be a rather ugly solution...

If it helps, here is the fragment's layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_height   = "wrap_content"
    android:layout_width    = "match_parent"
    android:orientation     = "vertical" >

    <EditText
        android:hint            = "@string/inputhint_feedbackName"
        android:id              = "@+id/input_feedbackName"
        android:imeOptions      = "actionNext"
        android:inputType       = "textPersonName"
        android:layout_height   = "wrap_content"
        android:layout_width    = "match_parent"
        android:lines           = "1"
        android:maxLines        = "1"
        android:singleLine      = "true" >
        <requestFocus />
    </EditText>

    <EditText
        android:gravity             = "top"
        android:id                  = "@+id/input_feedbackMessage"
        android:imeOptions          = "actionDone"
        android:layout_height       = "wrap_content"
        android:layout_marginBottom = "20dp"
        android:layout_width        = "match_parent"
        android:lines               = "10"
        android:inputType           = "textMultiLine" />

    <Button
        android:id                  = "@+id/button_feedbackSend"
        android:layout_height       = "wrap_content"
        android:layout_width        = "match_parent"
        android:text                = "@string/label_go"
        android:textSize            = "16sp"
        android:textStyle           = "bold" />
</LinearLayout>
Hindu answered 17/11, 2014 at 13:44 Comment(0)
B
63

I don't think it is possible to achieve this without using code.

Anyway I'm using this code inside my custom DialogFragment class. Hope it helps.

public void onResume() {
    super.onResume();

    Window window = getDialog().getWindow();
    Point size = new Point();

    Display display = window.getWindowManager().getDefaultDisplay();
    display.getSize(size);

    int width = size.x;

    window.setLayout((int) (width * 0.75), WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
}
Byte answered 19/2, 2015 at 0:47 Comment(3)
Awsome! that combines all the answers I've been reading for the last hour! ^_^Windfall
Added the same code in onCreateView() and nothing happened. Worked only after adding it to onResume(). Cheers to Android !!Hutch
worked for me on android studio 4.2 C15 SDK30Exobiology
O
9

You can achieve this by setting a style to your dialogfragment on it's onCreate method:

setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);

and in your styles.xml

   <style name="yourStyle" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/some_background</item>
        <item name="android:windowAnimationStyle">@null</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowMinWidthMajor">75%</item>
        <item name="android:windowMinWidthMinor">75%</item>
        <item name="android:windowNoTitle">true</item>
    </style>
Octans answered 16/3, 2018 at 19:25 Comment(1)
it didn't work for me on android studio 4.2 C15 SDK30Exobiology
N
0

Updated Code - 2021 - kotlin


override the onResume() method of your dialog fragment like below

override fun onResume() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val windowMetrics = 
        requireActivity().windowManager.currentWindowMetrics
        val insets: Insets = windowMetrics.windowInsets
            .getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
        val width = windowMetrics.bounds.width() - insets.left - 
        insets.right
        val height = windowMetrics.bounds.height() - insets.top - 
        insets.bottom
        val window = dialog!!.window
        if (window != null) {
            window.setLayout((width * 0.90).toInt(), (height * 
            0.90).toInt()) // for width and height to be 90 % of screen
            window.setGravity(Gravity.CENTER)
        }
        super.onResume()
    } else {
        val window = dialog!!.window
        val size = Point()
        // Store dimensions of the screen in `size`
        val display = window!!.windowManager.defaultDisplay
        display.getSize(size)
        // Set the width of the dialog proportional to 90% of the screen width
        window.setLayout((size.x * 0.90).toInt(), 
        ViewGroup.LayoutParams.WRAP_CONTENT)
        window.setGravity(Gravity.CENTER)
        // Call super onResume after sizing
        super.onResume()
    }
}
Nicaea answered 10/6, 2022 at 7:45 Comment(0)
I
0

you can override onStart and add this code to this block

        getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Implicatory answered 4/9, 2023 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.