Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an alert dialog fill 90% of screen size?

Tags:

android

dialog

I can create and display a custom alert dialog just fine but even so I have android:layout_width/height="fill_parent" in the dialog xml it is only as big as the contents.

What I want is dialog that fills the entire screen except maybe a padding of 20 pixel. Then the image that is part of the dialog would automatically stretch to the full dialog size with fill_parent.

like image 385
Fabian Avatar asked Feb 21 '10 16:02

Fabian


People also ask

How to make an Alert dialog fill 90 of screen size?

You can use percentage for (JUST) windows dialog width. All you need to do is extend this theme and change the values for "Major" and "Minor" to 90% instead 65%.

How to set Alert dialog size in Android?

You just have to give android:theme="@android:style/Theme. Dialog" in the android manifest. xml for your activity and can write the whole layout as per your requirement. you can set the height and width of your custom dialog from the Android Resource XML.

How do I make dialog full screen?

This example demonstrate about How to make full screen custom dialog. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


2 Answers

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT. To make the Dialog bigger, you can set those parameters to MATCH_PARENT.

Demo code:

    AlertDialog.Builder adb = new AlertDialog.Builder(this);     Dialog d = adb.setView(new View(this)).create();     // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)      WindowManager.LayoutParams lp = new WindowManager.LayoutParams();     lp.copyFrom(d.getWindow().getAttributes());     lp.width = WindowManager.LayoutParams.MATCH_PARENT;     lp.height = WindowManager.LayoutParams.MATCH_PARENT;     d.show();     d.getWindow().setAttributes(lp); 

Note that the attributes are set after the Dialog is shown. The system is finicky about when they are set. (I guess that the layout engine must set them the first time the dialog is shown, or something.)

It would be better to do this by extending Theme.Dialog, then you wouldn't have to play a guessing game about when to call setAttributes. (Although it's a bit more work to have the dialog automatically adopt an appropriate light or dark theme, or the Honeycomb Holo theme. That can be done according to http://developer.android.com/guide/topics/ui/themes.html#SelectATheme )

like image 125
nmr Avatar answered Sep 26 '22 12:09

nmr


Try wrapping your custom dialog layout into RelativeLayout instead of LinearLayout. That worked for me.

like image 26
cvarsendan Avatar answered Sep 22 '22 12:09

cvarsendan