Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the max height of a Layout in android?

I have to implement layout like this; The layout should wrap the content if the content within it is less. If the content goes beyond some size, like 300dp, it should be set to max heght(300dp) and that be able to scroll. Is there any properties to do like this, if not any workaround?

like image 323
Kantesh Avatar asked Jul 06 '11 05:07

Kantesh


1 Answers

You can find the display size of the device. according to the display size you can set layout params. for example, if the display size is less than 300dp , you can set height as wrap content. if more than 300dp means set height as 300dp.

here is the example code:

WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);


    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);


    //here you can get height of the device.
   Log.d("check", metrics.heightPixels+"");
    if(metrics.heightPixels< 300)
    {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    }
    else {
         this.getWindow().setLayout(LayoutParams.WRAP_CONTENT,300);

    }

hope this one can help you.

like image 106
Rathakrishnan Avatar answered Nov 16 '22 20:11

Rathakrishnan