Reading Layout size at application startup
android, android-layout, android-scrollview
Solution
You can do something like this:
Get a final reference to your ScrollView (to access in the onGlobalLayout() method). Next, Get the ViewTreeObserver from the ScrollView, and add an OnGlobalLayoutListener, overriding onGLobalLayout and get the measurements in this listener.
final ScrollView myScroll = (ScrollView)findViewById(R.id.my_scroll);
ViewTreeObserver vto = myScroll.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LayerDrawable ld = (LayerDrawable)myScroll.getBackground();
height = myScroll.getHeight();
width=myScroll.getHeight();
ViewTreeObserver obs = myScroll.getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
}
});
see more in this thread:
How to retrieve the dimensions of a view?
Problem
I need to retrieve the height of `ScrollView` defined in a layout xml file during activity startup. Which is the best practice to implement this. I've tried placing the code inside `onCreate()`, `onStart()` and `onResume()`. All gives height as 0 at startup. Is there any methods like `onFinishInflate()` for actvities? Here is my code: ``` myScroll=(ScrollView) findViewById(R.id.ScrollView01); int height=myScroll.getHeight(); ```