Search code examples
androidscreenslidedirection

android activity switch direction


when switching between two activities, screen slides between right to left. When I press back key, screen slides from right to left. Is it a way that when I press back key from an activity to change screen sliding direction?


Solution

  • Danny's solution could be made to work but it is overly complicated. The key method you want to learn about is overridePendingTransition().

    Here is main activity that I mocked up to use it. I made it transition vertically just to show that you can make transformations in any direction you like:

    package com.superliminal.test;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class ScreenTransitionTest extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button btnForwards = (Button) findViewById(R.id.btnForwards);
            btnForwards.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Activity currentActivity = (Activity) v.getContext();
                    Intent i = new Intent(currentActivity, NewScreen.class);
                    // Tell the new activity how return when finished.
                    i.putExtra("anim id in", R.anim.down_in);
                    i.putExtra("anim id out", R.anim.down_out);
                    currentActivity.startActivity(i);
                    // This makes the new screen slide up as it fades in
                    // while the current screen slides up as it fades out.
                    overridePendingTransition(R.anim.up_in, R.anim.up_out);
                }
            });
        }
    }
    

    Here is the implementation of the new screen:

    package com.superliminal.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class NewScreen extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.new_screen);
        }
    
        @Override
        public void onBackPressed() {
            this.finish();
            // Use exiting animations specified by the parent activity if given
            // Translate left if not specified.
            overridePendingTransition(
                getIntent().getIntExtra("anim id in", R.anim.left_in),
                getIntent().getIntExtra("anim id out", R.anim.left_out));
        }
    }
    

    Your layout files can be anything you like. You don't need a wrapper layer. Here is my main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#990000" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Main Activity" />
        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnForwards"
            android:text="Forward" />
    </LinearLayout>
    

    And here is my new_screen.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#009900" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="New Screen. Use back button to return." />
    </RelativeLayout>
    

    The only other things you need are animation XML files that you put in the res/anim folder.

    up_in.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="1000"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
    </set>
    

    up_out.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
    </set>
    

    down_in.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="1000"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
    </set>
    

    down_out.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="1000"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />
    </set>