0

I am giving a transition to activity B which will be called when some button on Activity A is clicked. After clicking button Activity B is appear from Right to Left. Now I want transition Left to Right on Activity B when back button is clicked. anyone knows how to do this. I am calling onBackPressed() when I want to go to Activity A.

This is Activity A

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnNext = (Button) findViewById(R.id.btn_go_next);

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, second.class));
            }
        });


    }`

This is Activity B

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mBtnGoBack = (Button) findViewById(R.id.btn_go_back);

        mBtnGoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

        overridePendingTransition(R.anim.right_slide, R.anim.left_slide);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.left_slide, R.anim.right_slide);
    }

`

This is my Left side <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="500" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="-100%" android:toYDelta="0%" /> </set>

This is my right side <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:duration="500" android:fromXDelta="100%" android:fromYDelta="0%" android:toXDelta="0%" android:toYDelta="0%" /> </set>

3
  • 4
    Possible duplicate of Custom Back Button Animation Commented Dec 30, 2015 at 9:23
  • Sorry friend this is not working my Activity B is also transit from Right to Left Commented Dec 30, 2015 at 9:27
  • Can you post your code which shows you going from activity A to activity B... Also your onBack code. and your XML transition files please. Commented Dec 30, 2015 at 9:29

1 Answer 1

0

You need to create a new folder right click on res folder create new folder name it anim. And you can put your animation xmls in it.

Use this xml in res/anim/

This is for left to right animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate android:fromXDelta="-100%" android:toXDelta="0%"
             android:fromYDelta="0%" android:toYDelta="0%"
             android:duration="700"/>
</set>

This is for right to left animation:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
  <translate
     android:fromXDelta="0%" android:toXDelta="100%"
     android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700" />
</set>

In your coding use intent like for left to right:

this.overridePendingTransition(R.anim.animation_enter,
                   R.anim.animation_leave);
In your coding use intent like for right to left

this.overridePendingTransition(R.anim.animation_leave,
                               R.anim.animation_enter);
Sign up to request clarification or add additional context in comments.

4 Comments

sorry bro this code I have in my XML file . not work
you have to do create xml like this res/anim/left_to_right.xml and put left to right animation. res/anim/right_to_left.xml and put right to left animation. this.overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
I have create same XML like this.
Add this.overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left); below your startActivity();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.