Material-dialogs: How to specify the width and height of the dialog??

Created on 9 Oct 2015  路  2Comments  路  Source: afollestad/material-dialogs

or specify a percentage of the screen size?

Most helpful comment

Because I've struggled with the same question I would like to provide a possible solution or workaround for this.

You can do this by extending the MaterialDialog-Class and then use the getWindow().setAttributes(WindowManager.LayoutParams a)-Method.
For example in my case I needed a simple, small Loading-Dialog which locks the screen and just show a rotating intermediate progress inside without any text.
Therefor I extended the MaterialDialog and called the Super-Constructor with the Progress-Definition. Then I altered the width in the LayoutParams of the Window and set the new LayoutParams. Keep in mind, that you have to define the width and height in pixels and so you have to convert your values.

public class LightLoader extends MaterialDialog {
    public LightLoader(Context context){
        super(new MaterialDialog.Builder(context).progress(true, 0));
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                                                       128, 
                                                       context.getResources().getDisplayMetrics());
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCancelable(false);
        this.show();
    }
}

By the way: Great library and thanks for your work!

All 2 comments

You can't, if you could it would be in the README.

Because I've struggled with the same question I would like to provide a possible solution or workaround for this.

You can do this by extending the MaterialDialog-Class and then use the getWindow().setAttributes(WindowManager.LayoutParams a)-Method.
For example in my case I needed a simple, small Loading-Dialog which locks the screen and just show a rotating intermediate progress inside without any text.
Therefor I extended the MaterialDialog and called the Super-Constructor with the Progress-Definition. Then I altered the width in the LayoutParams of the Window and set the new LayoutParams. Keep in mind, that you have to define the width and height in pixels and so you have to convert your values.

public class LightLoader extends MaterialDialog {
    public LightLoader(Context context){
        super(new MaterialDialog.Builder(context).progress(true, 0));
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                                                       128, 
                                                       context.getResources().getDisplayMetrics());
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCancelable(false);
        this.show();
    }
}

By the way: Great library and thanks for your work!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

palaima picture palaima  路  4Comments

timbremer picture timbremer  路  6Comments

eygraber picture eygraber  路  3Comments

azizimusa picture azizimusa  路  4Comments

RezaOruji picture RezaOruji  路  3Comments