Relative Layout Get Width/height Return 0
I need to get width and height of Relative layout, however I do not know when the Relative Layout is ready to provide me these two attributes. My activity has several tabs and I ca
Solution 1:
My suggestion is to use a global layout listener like this in onCreateView:
YOUR_LAYOUT_INSTANCE = view.findViewById(R.id.YOUR_LAYOUT_ID);...
YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
    @Override
    public void onGlobalLayout()
    {
        // gets called after layout has been done but before display.
        if (Build.VERSION.SDK_INT < 16) {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        // get width and height
    }
});
Solution 2:
addOnGlobalLayoutListener is deprecated use removeOnGlobalLayoutListener .For better handling extending the solution provided above by @fasteque
    YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            // gets called after layout has been done but before display.
            if (Build.VERSION.SDK_INT < 16) {
                        YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } 
            else {
                        YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
        // get width and height
        }
    });
Post a Comment for "Relative Layout Get Width/height Return 0"