Android Tutorial Custom transitions between activities

This short tutorial will explain how to create custom transitions between activities.

The transitions must be declared in an XML file as described here: Animation Resources | Android Developers

1. Declare the transition

You should put the animation files under Objects\Res\anim folder.
The files must be set to be read-only. Otherwise they will be deleted.

After changing the files you should click on Tools - Clean Project. This will cause the IDE to recreate the resources mapping file (R.java).

2. Call SetAnimation

You should add this code to your project (requires Reflection library):
B4X:
Sub SetAnimation(InAnimation As String, OutAnimation As String)
    Dim r As Reflector
    Dim package As String
    Dim in, out As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    in = r.GetStaticField(package & ".R$anim", InAnimation)
    out = r.GetStaticField(package & ".R$anim", OutAnimation)
    r.Target = r.GetActivity
    r.RunMethod4("overridePendingTransition", Array As Object(in, out), Array As String("java.lang.int", "java.lang.int"))
End Sub

After calling StartActivity or Activity.Finish you should call SetAnimation.
For example:
B4X:
StartActivity(Activity2)
SetAnimation("zoom_enter", "zoom_exit") 'without the xml extension

The attached example demonstrates the transition. You should press on Connect and then on one of the images.
Pressing on the back key when the second activity is visible will also start a transition.
The animations are based on this example: Animation.java | Android Developers
 

Attachments

  • FlickrViewer_WithActivityAnimation.zip
    11.1 KB · Views: 3,549

wl

Well-Known Member
Licensed User
Longtime User
Hi,

I tried the demo on my Galaxy S3 and on B4A v2.02. When I click on an image it takes a few seconds and then "Unfortunately, Flickr Viewer2 has stopped".

Thanks
 

wl

Well-Known Member
Licensed User
Longtime User
Hi,

Thanks ! Upgrading tot Reflection 2.2 did the trick !!
 

dclarkchem

Member
Licensed User
Longtime User
activity animation using reflect

Got up at 2 AM with a new idea and it works.
 
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
Hi Erel,

I tried your example, it worked on Android 4.0.4 but not on Android 2.3.4. Any ideas?
 

susu

Well-Known Member
Licensed User
Longtime User
HTC Sensation XE with Android 2.3.4: Not work.
Samsung Galaxy S3 with Android 4.0.4: Work.
 

susu

Well-Known Member
Licensed User
Longtime User
I don't think so because I can see transitions from another apps.
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
This short tutorial will explain how to create custom transitions between activities.

The transitions must be declared in an XML file as described here: Animation Resources | Android Developers

1. Declare the transition

You should put the animation files under Objects\Res\anim folder.
The files must be set to be read-only. Otherwise they will be deleted.

After changing the files you should click on Tools - Clean Project. This will cause the IDE to recreate the resources mapping file (R.java).

2. Call SetAnimation

You should add this code to your project (requires Reflection library):
B4X:
Sub SetAnimation(InAnimation As String, OutAnimation As String)
    Dim r As Reflector
    Dim package As String
    Dim in, out As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    in = r.GetStaticField(package & ".R$anim", InAnimation)
    out = r.GetStaticField(package & ".R$anim", OutAnimation)
    r.Target = r.GetActivity
    r.RunMethod4("overridePendingTransition", Array As Object(in, out), Array As String("java.lang.int", "java.lang.int"))
End Sub

After calling StartActivity or Activity.Finish you should call SetAnimation.
For example:
B4X:
StartActivity(Activity2)
SetAnimation("zoom_enter", "zoom_exit") 'without the xml extension

The attached example demonstrates the transition. You should press on Connect and then on one of the images.
Pressing on the back key when the second activity is visible will also start a transition.
The animations are based on this example: Animation.java | Android Developers

Hi @Erel !


I got a very strange problem here. The transitions are working in emulator but NOT in my real device (LG-P920). The code is the same: first installed in emulator and after installed in call phone using b4aBridge..

Any sugestion?
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
hi @Erel ,

I´ll test and tell you if works! I think that could be some issue of Lg Optimus family (The LG Android Version - even ics, which I use - is not very good).

Thanks!
 

ivan.tellez

Active Member
Licensed User
Longtime User
Wow, really nice effect for the apps.

Just a Suggestion, its better to put the SetAnimation code in the Activity_Pause, doing this will make the transitions every time, even when you use CallSubDelayed to open the second Activity.

:D
 
Top