[ACCEPTED]-get layout height and width at run time android-android-layout

Accepted answer
Score: 37

Suppose I have to get a LinearLayout width defined in 2 XML. I have to get reference of it by XML. Define 1 LinearLayout l as instance.

 l = (LinearLayout)findviewbyid(R.id.l1);
ViewTreeObserver observer = l.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                init();
            l.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);
        }
    });

protected void init() {
        int a= l.getHeight();
            int b = l.getWidth();
Toast.makeText(getActivity,""+a+" "+b,3000).show();
    } 
    callfragment();
}  
Score: 5

The width and height values are set after 6 the layout has been created, when elements 5 have been placed they then get measured. On 4 the first call to onSizeChanged the parms 3 will be 0 so if you use that check for it.

Little 2 more detail here https://groups.google.com/forum/?fromgroups=#!topic/android-developers/nNEp6xBnPiw

and here http://developer.android.com/reference/android/view/View.html#Layout

Here is how to 1 use onLayout:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int width = someView.getWidth();
    int height = someView.getHeight();
}
Score: 5

To get it work, you need to check whether 6 the desired height value is bigger than 5 0 - and first then remove the onGlobalLayout 4 listener and do whatever you want with the 3 height. The listener calls its method continuously 2 and by the first call it is not guaranteed 1 that the view is measured properly.

    final LinearLayout parent = (LinearLayout) findViewById(R.id.parentView);
    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int availableHeight = parent.getMeasuredHeight();
            if(availableHeight>0) {
                parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //save height here and do whatever you want with it
            }
        }
    });
Score: 3

You could add on layout change listener 5 to your layout and get the newest height 4 and width or even the one before last change.

Added 3 in API level 11

Add a listener that will 2 be called when the bounds of the view change due 1 to layout processing.

LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.my_linear_layout);
myLinearLayout.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) {
            // Preventing extra work because method will be called many times.
            if(height == (bottom - top))
                return;
            
            height = (bottom - top);
            // do something here...
           }
     });
Score: 0

A generic approach using Kotlin based on 1 MGDroid's answer for API 16+.

/**
 * Align height of a container from wrap-content to actual height at runtime.
 * */
private fun <T: ViewGroup> alignContainerHeight(container: T) {
    container.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {

            // Obtain runtime height
            val availableHeight = container.measuredHeight

            if (availableHeight > 0) {
                container.viewTreeObserver.removeOnGlobalLayoutListener(this)
                setContainerHeight(container, availableHeight)
            }
        }
    })
}

/**
 * Note: Assumes that the parent is a LinearLayout.
 * */
private fun <T : ViewGroup> setContainerHeight(container: T, availableHeight: Int) {
    val availableWidth = container.measuredWidth
    val params = LinearLayout.LayoutParams(availableWidth, availableHeight)

    // Note: getLayoutParams() returns null if no parent exists
    if (container.layoutParams != null) {
        container.layoutParams = params
    }
}

More Related questions