Hiren Patel Hiren Patel - 1 year ago 124
Android Question

How to get programatically / runtime width and height of Relative / Linear layout in android?

I required runtime dimensions of Relative/Linear Layout.


android:layout_height="match_parent" >


private int width, height;

RelativeLayout rlParent = (RelativeLayout) findViewById(R.id.rlParent);

w = rlParent.getWidth();
h = rlParent.getHeight();

Log.i("Width-Height", width+"-"+height);

Please share your thoughts.

Answer Source

Well I have implemented by this way:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout);
ViewTreeObserver viewTreeObserver = linearLayout.getViewTreeObserver(); 
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    public void onGlobalLayout() { 
        int width  = linearLayout.getMeasuredWidth();
        int height = linearLayout.getMeasuredHeight(); 


This example is for Linear layout, for Relative layout follow same process.

Hope this would help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download