Search code examples
androidtabsviewflipper

Android's ViewFlipper initializing views


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.


Solution

  • 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.