7

After screen rotating, the height of one of view object is changed, I want to know the height value in pixel.

The onConfigurationChanged method is likely called before view finishing rotation. So if I measure the view size in this method , the size is still the value of rotation before.

The problem is how can I get the view size value after rotation finish Without reconstructing Activity.

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
user2614801
  • 105
  • 1
  • 5
  • 1
    You can always check this in the `OnResume()` method, since it's called after the rotation. – g00dy Jul 26 '13 at 08:31
  • 1
    thank for your answer.It seems does not run OnResume()method since I use android:configChanges="orientation|screenSize" in AndroidManifest.xml file. I set the breakpoint in OnResume ,but didnot stop when ratation. – user2614801 Jul 26 '13 at 08:39

1 Answers1

4

One possible solution:

private int viewHeight = 0;
View view = findViewByID(R.id.idOfTheView);
view.getViewTreeObserver().addOnGlobalLayoutListener( 
    new OnGlobalLayoutListener(){
        @Override
        public void onGlobalLayout() {
            viewHeight = view.getHeight();
        }

OR

@Override
public void onResume(){
  int viewHeight = 0;
  View view = findViewByID(R.id.idOfTheView);
  viewHeight  = view.getHeight();
}
g00dy
  • 6,752
  • 2
  • 30
  • 43
  • thank you for your answer, I think the onResume() won't run since I set **android:configChanges="orientation|screenSize"** in AndroidManifest.xml file. I will study the way of OnGlobalLayoutListener. – user2614801 Jul 26 '13 at 09:07
  • `OnResume()` is always called when these's an orientation change, because the whole Activity gets re-created. – g00dy Jul 26 '13 at 09:17
  • I just read the programming guide article about rotation again. It said that above the API level 12 , configuration change does not restart your activity, the reference’s address is http://developer.android.com/guide/topics/resources/runtime-changes.html in this article there is a paragraph like this Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged(). ...... – user2614801 Jul 26 '13 at 12:09
  • thanks ,the problem is solved by adding GlobalLayoutListener. – user2614801 Jul 29 '13 at 02:44