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);
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.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With