145

I am using an activity with the dialog theme set, and I want it to be full screen. I tried all sorts of things, even going through the WindowManager to expand the window to full width and height manually, but nothing works.

Apparently, a dialog window (or an activity with the dialog theme) will only expand according to its contents, but even that doesn't always work. For instance, I show a progress bar circle which has width and height set to FILL_PARENT (so does its layout container), but still, the dialog wraps around the much smaller progress bar instead of filling the screen.

There must be a way of displaying something small inside a dialog window but have it expand to full screen size without its content resizing as well?

mxk
  • 43,056
  • 28
  • 105
  • 132
  • 2
    Interesting note: When setting the layout container's width and height to an actual value in pixels, then the dialog resizes accordingly. But when setting it to FILL_PARENT, then it doesn't... – mxk Sep 01 '09 at 14:30
  • I sort of solved the problem by using a workaround, but since this is not a good solution, I'll leave the question open. What I did was to subclass LinearLayout and override its onMeasure() method, which is responsible for determining the size of the view during a layouting cycle, and fix it to the screen width and height. This is not correct though, since this does not take into account the space occupied by the menu bars, so the container will actually be taller than it looks like on the screen (with all the weird side effects like children disappearing in non visible areas of the screen). – mxk Sep 04 '09 at 08:16
  • I found the correct solution, see my answer below. – mxk Nov 07 '09 at 17:08
  • Really? The layout the activity is instantiating, does it have a parent Relative or LinearLayout tag that is set to height and width of fill_parent? I've implemented a few activities with a dialog theme and never had a problem with it taking up the entire screen. – MattC Sep 02 '09 at 20:43
  • Yes, the activity has a LinearLayout set to FILL_PARENT for both width and height. – mxk Sep 04 '09 at 08:04
  • see this answer in other thread, maybe it could help http://stackoverflow.com/a/7655350/3214497 – Hugo Mar 04 '15 at 17:15

10 Answers10

327

I found the solution:

In your activity which has the Theme.Dialog style set, do this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

It's important that you call Window.setLayout() after you call setContentView(), otherwise it won't work.

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
mxk
  • 43,056
  • 28
  • 105
  • 132
  • 5
    Thanks, saved my time , nowhere I would have figured this out ! but any idea why we have to explicitly specify in code , why fill_parent does not work through xml for activities with theme dialog ? – sat Feb 08 '11 at 12:52
  • 3
    My dialog won't fill width, not by XML, and not by this method. I'm eating my nails. – eitama Oct 12 '11 at 08:37
  • 6
    And it is: `import android.view.ViewGroup.LayoutParams;` –  Mar 20 '12 at 07:32
  • Cool its really work. But not getting why we need to define explicitly. As i have already define fill_parent in xml. – Mihir Palkhiwala May 03 '12 at 11:03
  • Works great, But you may prefer to use LayoutParams.WRAP_CONTENT especially for the height (2nd parameter). – StackOverflowed Aug 02 '12 at 06:00
  • 11
    FILL_PARENT is deprecated now, use MATCH_PARENT instead – AlexV Feb 28 '13 at 12:53
  • Not all right! dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); – Albert.Qing Oct 15 '13 at 09:32
  • 4
    It's not filling the entire screen, it's like it has a small padding on each side. – Machado Jul 06 '15 at 20:29
  • 2
    i want a full screen width dialog, i used **dialog.getWindow()** and also **getWindow()**, but my dialog still not taking full width of screen, still behind screen is showing from both sides(left & right). any solution for dialog ? – Rahul Sharma Aug 24 '15 at 06:57
  • I don't know if it's work for default dialog style, but i am using Theme.AppCompat.Light.Dialog and it give padding to each sides(top, right, bottom and left). I need to use Theme.AppCompat.Light.Dialog, because i use AppCompatActivity to support lower android versions. – HendraWD Sep 20 '16 at 08:26
  • 1
    If you guys still struggle with padding on the dialog itself, you can see my answer below http://stackoverflow.com/a/39590004/3940133 – HendraWD Sep 20 '16 at 09:12
  • Can i customize the size for it? – PJ2104 Jul 04 '18 at 13:38
73

You may add this values to your style android:windowMinWidthMajor and android:windowMinWidthMinor

<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
    ...
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
</style>
Serhii Nadolynskyi
  • 5,473
  • 3
  • 21
  • 20
54

I just want to fill only 80% of the screen for that I did like this below

        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int screenWidth = (int) (metrics.widthPixels * 0.80);

        setContentView(R.layout.mylayout);

        getWindow().setLayout(screenWidth, LayoutParams.WRAP_CONTENT); //set below the setContentview

it works only when I put the getwindow().setLayout... line below the setContentView(..)

thanks @Matthias

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
13

Wrap your dialog_custom_layout.xml into RelativeLayout instead of any other layout.That worked for me.

Ayan
  • 8,192
  • 4
  • 46
  • 51
  • 1
    Same here, Width and Height were completely wrong and couldn't figure out how to fix. Converting the root layout to a RelativeLayout fixed my issues as well! – SharpMobileCode Sep 15 '17 at 18:27
9

For Dialog This may helpful for someone. I want a dialog to take full width of screen. searched a lot but nothing found useful. Finally this worked for me:

mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);

after adding this, my dialog appears in full width of screen.

Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
7

This answer is a workaround for those who use "Theme.AppCompat.Dialog" or any other "Theme.AppCompat.Dialog" descendants like "Theme.AppCompat.Light.Dialog", "Theme.AppCompat.DayNight.Dialog", etc. I myself has to use AppCompat dialog because i use AppCompatActivity as extends for all my activities. There will be a problem that make the dialog has padding on every sides(top, right, bottom and left) if we use the accepted answer.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

On your Activity's style, add these code

<style name="DialogActivityTheme" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@null</item>
</style>

As you may notice, the problem that generate padding to our dialog is "android:windowBackground", so here i make the window background to null.

HendraWD
  • 2,984
  • 2
  • 31
  • 45
2

Set a minimum width at the top most layout.

android:minWidth="300dp"

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">

<!-- Put remaining contents here -->

</LinearLayout>
roy mathew
  • 7,782
  • 4
  • 29
  • 40
2

Matthias' answer is mostly right but it's still not filling the entire screen as it has a small padding on each side (pointed out by @Holmes). In addition to his code, we could fix this by extending Theme.Dialog style and add some attributes like this.

<style name="MyDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
</style>

Then we simply declare Activity with theme set to MyDialog:

<activity
    android:name=".FooActivity"
    android:theme="@style/MyDialog" />
DYS
  • 2,826
  • 2
  • 23
  • 34
2

This would be helpful for someone like me. Create custom dialog style:

<style name="MyDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

In AndroidManifest.xml file set theme for wanted activity:

<activity
        android:name=".CustomDialog"
        ...
        android:theme="@style/MyDialog"/>

That is all, no need to call methods programaticaly.

Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • still show padding on each sides(top, right, bottom and left) and if we need to show status bar too, this solution will fail – HendraWD Sep 20 '16 at 08:32
  • Check https://stackoverflow.com/a/39590004/3940133 if you have padding on each sides – HendraWD Dec 06 '19 at 04:35
0

In your manifest file where our activity is defined

<activity
        android:name=".YourPopUpActivity"
        android:theme="@android:style/Theme.Holo.Dialog" >
    </activity>

without action bar

<activity android:name=".YourPopUpActivity"
            android:theme="@android:style/Theme.Holo.Dialog.NoActionBar"/>
Makvin
  • 3,475
  • 27
  • 26