Slide Activity

aaronk

Well-Known Member
Licensed User
Longtime User
This might help..

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%" />
 
Upvote 0
Top