-1

In my application I have thee tabs in the ViewFlipper. When the activity is started I initialize all the tabs and the views inside of them.

But the problem is at the 3th tab. On the initialization of this tab, I want to know the distance of an TextView to it parents left side. Therefor I call the TextView.getLeft().

But when this tab isn't shown I receive '0' from this method. Therefor I can not initialize the view correctly.

Now it becomes strange: When I start the activity to the first tab, then go to the third, the getLeft return 0 of startup and on tab click.... But when I start the activity to the third tab, the getLeft returns a valid value....

So, how can I get the correct value? I guess the ViewFlipper doesn't have the correct X,Y coordinates of views when the tab isn't shown.

Ps. It was hard to explain. If I need to clarify something, please let me know.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50

2 Answers2

0

Try this in your on create

ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        Log.e("Width",Integer.toString(textview.getWidth()));

    }
});

onCreate is called before the ui is drawn so the width should be 0 as measure or drawing hasn't happened yet.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • Thanks, I tried this is only called when the activity got started. Not when the tab flips. And I still received 0. – Ion Aalbers Jan 11 '12 at 12:33
  • Remove this line textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);. Or might need to try something onTabChangedListener. – blessanm86 Jan 11 '12 at 12:42
0

I noticed that the getLeft was called before! the tab actually got executed. (The call was after setDisplayedChild() )

Therefor I needed to know when the view is actually shown. I added an AnimationListener on the InAnimation:

ViewFlipper.getInAnimation().setAnimationListener(new AnimationListener() {

    public void onAnimationStart(Animation animation) {
         TextView.getLeft();
    }

    public void onAnimationRepeat(Animation animation) {}
    public void onAnimationEnd(Animation animation) {}
});

I don't really like this sollution. But it works.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50