Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dialog which will be full in horizontal dimension

When I use in layout, which specifies this dialog android:layout_width="match_parent" I get this dialog:

small dialog

I need dialog which will be wider. Any ideas?

like image 477
west44 Avatar asked Jul 23 '12 13:07

west44


People also ask

How do I make alert dialog fill 90% of screen size?

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.

How do I set the height and width of AlertDialog in android programmatically?

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.


2 Answers

You can do that by grabbing the Window object that the dialog uses, and resetting the width. Here's a simple example:

//show the dialog first
AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Test Dialog")
        .setMessage("This should expand to the full width")
        .show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);

Here's the end result. Note that there's a lot more styling you can do to the dialog if you need to fine tune things (like change the background, etc). When I've had to do this in the past, I usually use this method, and customize the layout used in the dialog with setView() on the builder class.

example

like image 59
wsanville Avatar answered Oct 29 '22 07:10

wsanville


Another way to fix this is to use:

import android.support.v7.app.AlertDialog;

instead of:

import android.app.AlertDialog;
like image 4
user2880229 Avatar answered Oct 29 '22 08:10

user2880229