Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set width of custom dialog to wrap content android

I want to set the width of custom dialog to wrap content but always it fill all the width of the screen

I have tested this

android.view.WindowManager.LayoutParams params = mydialog.getWindow().getAttributes();
params.width = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
mydialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

and that

mydialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
like image 503
begiPass Avatar asked Jan 21 '14 12:01

begiPass


People also ask

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

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.)


1 Answers

I know this question is old, but if someone doesn't want to add WindowManager.LayoutParams then just set theme of the dialog to android.R.style.Theme_Holo_Light_Dialog_NoActionBar this will make the dialog to wrap it's width to its contents.

Example:

AlertDialog alert = new AlertDialog.Builder(context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar).create();

Make sure that the layout you are inflating is set to wrap_content for both height and width.

like image 156
Mustansir Avatar answered Sep 27 '22 21:09

Mustansir