Slide Animations when switching from one activity to another – Java Android

Create a anim/ folder inside res/ in your Android Project and create the following files:

slide_in_left.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”-100%”
        android:fromYDelta=”0%”
        android:toXDelta=”0%”
        android:toYDelta=”0%” />

</set>

slide_in_right.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”100%”
        android:fromYDelta=”0%”
        android:toXDelta=”0%”
        android:toYDelta=”0%” />


</set>

slide_out_left.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”0%”
        android:fromYDelta=”0%”
        android:toXDelta=”-100%”
        android:toYDelta=”0%” />


</set>

slide_out_right.xml

<set xmlns:android=”http://schemas.android.com/apk/res/android”
    android:shareInterpolator=”false” >

    <translate
        android:duration=”200″
        android:fromXDelta=”0%”
        android:fromYDelta=”0%”
        android:toXDelta=”100%”
        android:toYDelta=”0%” />


</set>

Then you can use animations like this:

Intent intent = new Intent(FirstAct.this, SecondAct.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

Bye!


Handle Swipe Gestures in Java Android: Right to Left (Forward)

Hi guys,

Instead of using a ViewPager you can handle touch gestures like this (Useful when you have only two views and you’d like to swipe only in a single direction – For example: Only Right to Left to go forward):

 //This method is called everytime the user touches the screen
    public boolean onTouchEvent(MotionEvent touchevent) {
        switch (touchevent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
            case MotionEvent.ACTION_UP:
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                if (x1>x2) {
                    Log.i(“log_tag”, “Right to left Swipe Detected!”);
                }
                break;
        }
        return false;
    }