max max - 3 months ago 11
Android Question

How to create layout withminimum and maximum height?

I have two layout layout1 and layout2 in linear layout with vertically gravity.

standard layout1 height for me is for example 300dp (big height for small displays).

In small displays this takes almost all of the view height. But I want to take layout1 maximum height 50% of the view.

If I set height 50% of all height view in big displays I have some loss space.

enter image description here

If I set height 300dp in small displays I just have layout1.

enter image description here

So I have to limit for layout1 height, 50% for weight in small displays and 300dp for dpi in large displays.

how can I set this limits to my layout1.


At runtime, you can determine how much space your LinearLayout1 is taking up, and adjust its height only if it takes more than half of the screen. To do this, use following code.

Suppose you have given your LinearLayout1 the id as, then using this code, you can ask android to adjust height of LL1 to 50%

int screenHeight, screenWidth;

//Code to determine screen's height and width.

Display display = getWindowManager.getDefaultDisplay();
if (android.os.Build.Version.SDK_INT>=13) {
    Point size = new Point();
    screenHeight = size.x;
    screenWidth = size.y;
else {
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();

LinearLayout ll = (LinearLayout) findViewById(;

int layoutHeight = ll.getLayoutParams().height; // gets layout's height

if ((layoutHeight * 2) > screenHeight) { 

    // true when LL1 takes more than half of screen, good time to set it back to 50% height

    ll.getLayoutParams().height = screenHeight/2;
    ll.requestLayout(); //Forces layout to be adjusted.

else {
//All good, no work required, so skip this else block