Home » Android » android – How can I set maximum height to Dialog?

android – How can I set maximum height to Dialog?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I wanna set maximum height of dialog. Not custom height like that set by dp or px. I wanna set the greatest possible height to dialog relatively current device screen size.

How to&Answers:

You can not set max height directly. Just an alternate, you can reset height if its height is greater than maximum height you want to set.

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
int dialogWidth = lp.width;
int dialogHeight = lp.height;

if(dialogHeight > MAX_HEIGHT) {
   d.getWindow().setLayout(width,MAX_HEIGHT);
} 

Answer:

Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_example);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
//lp.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 330/*height value*/, getResources().getDisplayMetrics()); for custom height value
dialog.getWindow().setAttributes(lp);
dialog.show();

I think this can solve it. I’ve added two way one is for set dialog height with match parent property and second one is for setting height with custom value

Answer:

may be addOnLayoutChangeListener() is usefull for you.

you can add it before or after dialog.show()

this is my code :

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final View view = LayoutInflater.from(this).inflate(R.layout.custom_alertdialog, null);
    builder.setMessage("TestMessage xxxxxxx");
    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    builder.setView(view);  //在setView之前调用builder的原有设置控件方法时,能展示设置的控件,之后设置的则不展示!!
    AlertDialog dialog = builder.create();
    dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_bk_cnoneralert));
    //builder.show();   //用这个的话,背景并不会改变,依旧是默认的

    dialog.show();  //必须用这个show 才能显示自定义的dialog window 的背景

    //这种设置宽高的方式也是好使的!!!-- show 前调用,show 后调用都可以!!!
    view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                                   int oldRight, int oldBottom) {
            int height = v.getHeight();
            int contentHeight = view.getHeight();

            LogUtils.e("高度", height + " / " + " / " + contentHeight);
            int needHeight = 500;

            if (contentHeight > needHeight) {
                //注意:这里的 LayoutParams 必须是 FrameLayout的!!
                //NOTICE : must be FrameLayout.LayoutParams 
                view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, 
                        needHeight));
            }
        }
    });

Answer:

DisplayMetrics displaymetrics = new DisplayMetrics();

getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(displaymetrics);
int width = (int) (displaymetrics.widthPixels);
int height = (int) (displaymetrics.heightPixels);
d.getWindow().setLayout(width,height);
d.show();

Where d is dialog. This code sets the dialog to full screen.