1

I have an ActivityGroup meant to develop a tabHost. One of the tabs contains two activities into it. The problem is that when I move between this two activities there is no sliding between them. The old activity dissappears and the new one comes automatically on screen.

What I need to implement is this - when I move from activity2 back to activity 1, the activity2 has to leave the screen by sliding to the right and the new activity(activity 1) has to come into the screen by sliding from left to right.

The effect is similar to android default style when an intent is used to move between activities!

What I have achieved so far is:

when I try to move from activity 2 to activity 1: activity 2( the old one) dissapears from the screen, the screen turns black and the new activity comes into the screen by sliding from left to right. It would be great if I would manage to make the old activity to leave the screen by sliding too.

This is my code for coming back from the old activity to the new one:

Animation animation = null;

 Window oldWindow = manager.getCurrentActivity().getWindow();

 if(oldWindow != null)
 {
 animation = AnimationUtils.loadAnimation(this, R.anim.right_to_left);    
 View v =oldWindow.getDecorView();
 v.startAnimation(animation);

 }

 manager.destroyActivity(mIdList.get(index), true);
 mIdList.remove(index); index--;

 String lastId = mIdList.get(index);
 lastIntent = manager.getActivity(lastId).getIntent();
 newView = manager.startActivity(lastId, lastIntent).getDecorView();
 newView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right));

 if(newView != null)
 {
 setContentView(newView);
 }

One big bug is this: if I remove this line from my code: `setContentView(newView)` I get the desired effect for the old activity. Meaning the old activity leaves the screen by sliding, but the new one never comes in.

If I put that line back the old activity doesn't do any sliding just dissapears!!!!

I use a Samsung Galaxy for testing and I tried to use overridePendingTransition but it has no effect on the animations.

Here are my xml files:

right_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="true">
      <translate
       android:fromXDelta="0%" android:toXDelta="100%"
       android:fromYDelta="0%" android:toYDelta="0%"
       android:duration="3500" />
    </set>


 left_to_right.xml


 <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="true">
        <translate android:fromXDelta="-100%" android:toXDelta="0%"
          android:fromYDelta="0%" android:toYDelta="0%"
         android:duration="3500"/>
       </set>

Thank you. Hope you can help me.

adrian
  • 4,574
  • 17
  • 68
  • 119

2 Answers2

1

You can achieve this using tabs with ViewPager.

Also check out this project. You'll find what you need and more.

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • Can viewpager do sliding between activities? – adrian Jun 28 '12 at 07:25
  • Between Fragments actually, but that's the effect you're looking for. Check out the [demo](https://play.google.com/store/apps/details?id=com.actionbarsherlock.sample.fragments) for the first link and look for "Tabs and Pager". Also check out the [demo](https://play.google.com/store/apps/details?id=com.viewpagerindicator.sample) for the second link. – Benito Bertoli Jun 28 '12 at 07:32
  • One more thing - what library should I add for the first project?? I imported it in my eclipse but I get full of errors and it requests some library in my build path! – adrian Jun 28 '12 at 07:39
  • The first one is part of the [ActionBarSherlock](https://github.com/JakeWharton/ActionBarSherlock) demo. If you don't need an actionbar go with the second. For both you'll need the [Android Support Library](http://developer.android.com/tools/extras/support-library.html). – Benito Bertoli Jun 28 '12 at 07:52
  • Hey, do you know is there any way I could increase the speed of scrooling for viewpager? if yes, how? Thank you – adrian Jun 28 '12 at 14:43
  • Yes. You have to change the animation duration in the Scroller of the ViewPager. Check [this](http://stackoverflow.com/a/9731345/1117415) out. – Benito Bertoli Jun 28 '12 at 14:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13171/discussion-between-adrian-and-benito) – adrian Jun 28 '12 at 14:57
0

Use ViewAnimator class

This will allow you to animate freely

Moog
  • 10,193
  • 2
  • 40
  • 66
  • can you give me an example of how to use it? Thank you very much – adrian Jun 28 '12 at 07:27
  • sure but you could have googled this http://www.qubi.us/2011/09/easy-viewanimator-transition-slide.html – Moog Jun 28 '12 at 17:59
  • I tried to make it animate automatically and moved the code in onCreate() but unfortunately it doesn't work! – adrian Jun 29 '12 at 07:10