Get size of frame in ActivityFragment



I am trying to get the size of fragment in my FrameLayout with this code:



setContentView(R.layout.main);
frame=(FrameLayout)findViewById(R.id.fragment1);

frame.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
width=frame.getWidth();
height = frame.getHeight();

frame.getViewTreeObserver().removeGlobalOnLayoutListener( this );
}

});


this works, but for the first call of main I get 0, zero value? and on the other calls i get the value from the layout view before.


lets say the fragment is PORTRAIT: 100 x 200, and LAND: 200:300 when I call the activityfragment in portrait first time i get 0 x 0 when i turn my device (LAND) i get 100 x 200 (the value for portrait)


my width and height are like this in main:



public static int height;
public static int width;


I think the problem is, the code works but the fragment1 Fragment Code get's the widht/height value earlier than the values are saved in main... like this



height.setText(String.valueOf(MainActivity.height));
width.setText(String.valueOf(MainActivity.widht));


EDIT:


It's not because of values are saved to late... i tested it.. it's because the first value of size is 0 x 0. hmm


No comments:

Post a Comment