Animations for activities

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I have two a activities and I am trying to animate them..

Activity 1
Main:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main") 'name of my layout file
End Sub
Sub open_Click
    StartActivity(Main2)
End Sub

Activity 2
Main2:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main2") 'name of my layout file
End Sub

When I tap on the button (open_click) it loads my second Activity (Main2) which works, but I want to make it so that the Activity that was already opened (Main) to move to the left while the other activity opens (Main2) from the right.

I have seen the Custom Activity Animation post but it looks like the activity / image fades over the top but I want it to slide left or right.

Does anyone know how to do this?
 

aaronk

Well-Known Member
Licensed User
Longtime User
Hello,

I ended up getting it working.

I played around with the XML files.

Incase someone else wants to know how to do it here is how to do it..

The following will allow you to tap on a button and it will load the new Activity, and while the new activity loads it will animate the view left and right.

Activity 1- Named 'Main'
'Main2' is the name of my Activity that I want to load, when I tap on the button
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main") 'name of my layout file, so it displays something on the screen
End Sub
Sub Button1_Click
    StartActivity(Main2)
    SetAnimation("file3", "file4") 'move the current Activity to the left, and the new activity will come from the right and will scroll to the left
End Sub

Activity 2 - Named 'Main2'
Main2:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main2") 'name of my layout file, so it displays something on the screen
End Sub
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Activity.Finish
        SetAnimation("file2", "file1") 'move the current Activity to the right, and the new (or known as the old Activity) will come from the left and will scroll to the right
        Return True
    End If
End Sub

Code Module:
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

The following XML files need to be placed in ..\Objects\res\anim folder and all need to be set to 'Read-Only' (right hand mouse on the file and select 'properties' then tick the box that says 'Read-Only')

file1.xml:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0%"
    android:toXDelta="100%" />

file2.xml:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="-100%"
    android:toXDelta="0%" />

file3.xml:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="100%"
    android:toXDelta="0%" />

file4.xml:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0%"
    android:toXDelta="-100%" />
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Up and Down activity animation

Hello guys, thank you very much for this awesome example, however i need to make my activities move from top to bottom, and bottom to top, how would i go around accomplishing this, any help will be greatly appreciated.

Thanks,
Walter
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
I think it will be easier if you use Animation library (link).
 
Upvote 0

fadas

New Member
hi everyone
please help me
i did every thing that aaronk say ed
but it dosent work , why
i have reflection 1.9 is that cause ?
 
Upvote 0

susu

Well-Known Member
Licensed User
Longtime User
The animations library doesn't animate activities.

Why not? I used this code to animate the activity:
B4X:
Dim Ani As Animation

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout")
   Ani.InitializeScaleCenter("", 0,0,1,1, Activity)
   Ani.Duration = 500
   Ani.Start(Activity)
End Sub
 
Upvote 0
Top