2

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.

holder.questions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(DoctorsProfile.this,Questions.class);
            i.putExtra("DOCTOR_ID",doctor_id);
            startActivity(i);
            overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);

        }
    });

overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.

4 Answers4

2
Fragment fragment = Fragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
            R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
            R.anim.fragment_slide_right_exit);
    Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
            .content_frame);
1

This tip features how to change Android’s default animation when switching between Activities. The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity. Here’s an example: //Calls a new Activity

startActivity(new Intent(this, NewActivity.class));

//Set the transition -> method available from Android 2.0 and beyond

overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);

These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.

for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
  • for example code visit: http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483 hope it will help you. – Ram Koti Mar 17 '17 at 06:45
1

Change like this code you must be passing activity as context in adapter

holder.questions.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    Intent i = new Intent(DoctorsProfile.this,Questions.class);
            i.putExtra("DOCTOR_ID",doctor_id);
            Activity activity = (Activity) context;
    activity.startActivity(i);
    activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
        }
    });

Note : Context is the Base Object of Activity

Update : I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .

Alternative : Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.

Ravindra Shekhawat
  • 4,275
  • 1
  • 19
  • 26
0

There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ask_question);
    overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
  }
Abhishek Kumar
  • 1,255
  • 13
  • 21