0

I've written an android app with a view within an activity that relies on a callback to the view's wrapper's getHeight() function using an OnGlobalLayoutListener attached to the view's wrapper's viewTreeObserver to determine the amount of space it has to work with, during onCreate. Usually this is less than the 400px requested by chordDisplay in the xml below. This works perfectly in the android emulator, for a variety of screen sizes using android 2.1 and 4.03. However, on my kindle fire, the callback does not initially return the correct height when I launch the app in landscape (less than 400px are available) until I switch the orientation to portrait and back to landscape either manually or by code. This means the contents of my view aren't sized correctly initially.

sample logcat output when I initially launch the app in landscape:

  04-22 17:31:28.249: D/onGlobalLayout(12979): chordDisplay.getHeight(): 400

then I switch to portrait and back to landscape:

  04-22 17:32:44.546: D/onGlobalLayout(12979): chordDisplay.getHeight(): 350

I don't understand how this could be happening considering that all that happens during an orientation change is another call to onCreate() in the app, right? Which is the same thing that happens when I start the app.

Does anyone have any experience with similar orientation switching / layout bugs in their apps?/Have any ideas why this could be happening? Any help would be appreciated... Thanks.

code:

OnGlobalLayoutListener thelistener;
RelativeLayout chordDisplayWrapper;
TableRow.LayoutParams lp;
private ChordAdapter chordAdapter;
private HorizontalListView chordDisplay

thelistener = new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                chordDisplayWrapper.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                Log.d("onGlobalLayout", "chordDisplay.getHeight(): " + chordDisplay.getHeight());
                lp = new TableRow.LayoutParams(chordDisplay.getHeight()/6 -2,chordDisplay.getHeight()/6 -2); //6 just works.  I shrink it a tiny bit to compensate for some weird bug.
                chordAdapter = (new ChordAdapter(mcontext, R.layout.row, magicbundle, lp));
                chordDisplay.setAdapter(chordAdapter);
            }
        };
        chordDisplayWrapper.getViewTreeObserver().addOnGlobalLayoutListener(thelistener);

xml layout for views involved:

   <RelativeLayout
        android:id="@+id/chordDisplayWrapper"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center" >

        <berry.chordsfree.HorizontalListView
            android:id="@+id/testerList"
            android:layout_width="wrap_content"
            android:layout_height="400px"
            android:layout_centerInParent="true"
            android:gravity="center" />
   </RelativeLayout>
Nick
  • 9,285
  • 33
  • 104
  • 147
vancan1ty
  • 563
  • 1
  • 4
  • 16

1 Answers1

1

You may have solved this by now, but posting for posterity for those that may come across this after -

I'm having the same problem on my Nexus One, running a custom 2.3.7 ROM, but the code works on my Nexus Galaxy on stock 4.1.1.

Similar to you, I'm trying to resize certain views during onCreate using ViewTreeObserver. I've tried manually calling dispatchGlobalLayout and have ascertained it isn't an issue with onGlobalLayout not firing. And I played around with moving elements into the activity's onWindowFocusChanged without success (but I want to avoid having much resizing done there). This has cost me more than a day of work so I'm just using a quick/dirty fix for now. It appears normally if I just tweak the layout/view in question, after onCreate finishes:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (!reset) {

        LinearLayout frameItem = (LinearLayout) findViewById(R.id.frameItem);
        frameItem.setVisibility(LinearLayout.GONE);
        frameItem.setVisibility(LinearLayout.VISIBLE);
        reset = true;
    }
}
Frank Yin
  • 1,920
  • 1
  • 17
  • 12