How do I set the maximum height in Dialog?

I want to set the maximum height of the dialog. Custom height like dp or px. I want to set the maximum possible height for a dialog relative to the current screen size of the device.

+3


source to share


4 answers


You cannot set the maximum height directly. Alternative only, you can reset the height if its height is greater than the 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);
} 

      

+2


source


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 might solve it. I added two ways, set the height of the dialog with the parent property to match, and the second is to set the height with a custom value

+3


source


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 dialogue. This code sets the dialog to full screen.

+1


source


maybe addOnLayoutChangeListener () is helpful 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));
            }
        }
    });

      

+1


source







All Articles