west44 west44 - 1 year ago 80
Android Question

How to create dialog which will be full in horizontal dimension

When I use in layout, which specifies this dialog

I get this dialog:

small dialog

I need dialog which will be wider. Any ideas?

Answer Source

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")
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download