I'm trying to add an animation to my TabActivty. For example, when the user select the 2nd tab, I want the new activity comes from the right. When the user select the first tab, I want the activity comes from the left.
I've found how to add one animation, but I want to add one again. Here is the code I'm using:
public Animation inFromRightAnimation()
{
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(240);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
And
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId)
{
View currentView = getTabHost().getCurrentView();
currentView.setAnimation( inFromRightAnimation() );
}
});
How can I do that?
Thanks.
Regards.
V.
If you like, you can use Android Support Package - http://developer.android.com/sdk/compatibility-library.html
With little effort you can modify your activity to use fragments so your tabs can have transition animations just like the YouTube app. Here is a sample code of how to implement it - http://developer.android.com/sdk/compatibility-library.html
Edit: If you don't want to use support package, maybe this implementation will help
private class MyGestureDetector extends SimpleOnGestureListener {
and then on your tab changed listener just load the appropriate animation since you know which one was selected before the gesture, and the one that we are switching to after.
Also you need to catch the gesture by overriding the onTouchListener with you custom gesture detector (and maybe account for different screen density when determining if a gesture is a swipe action)
Sorry for the long answer, but I hope it helps :)
This works correctly:
And the animations:
You have to use
String tabId
and checkif
thistabId==firstTab
then put animation from leftelse
animation from right.I wrote a custom OnTabChangeListener based on this code that I wanted to share. Hopefully someone can use it :). Credit goes to Vomenki for original code.