Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set maximum height to Dialog?

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.

like image 707
Nick Avatar asked Apr 06 '17 14:04

Nick


People also ask

How to set max height of Dialog in android?

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.

How can I make alert dialog fill 100% of screen size?

If your dialog is made out of a vertical LinearLayout, just add a "height filling" dummy view, that will occupy the entire height of the screen. Nice!

How to set max height of div using jQuery?

jQuery: Calculate max height and apply to all element using jQuery. Here equalheight is parent class to find intro class and set equal height to all. maxHeight = $(this).


Video Answer


1 Answers

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));
            }
        }
    });
like image 106
CnPeng Avatar answered Sep 29 '22 10:09

CnPeng