Start your Activity with Slide Animation

Rohit Singh
2 min readDec 23, 2018

--

How to do this?

Let’s say you have two activities.

  • MovieDetailActivity
  • AllCastActivity

And on click of a Button, this happens.

You can achieve this in 3 simple steps

1) Enable Content Transition

Go to your style.xml and add this line to enable the content transition.

<item name=”android:windowContentTransitions”>true</item>

2) Write Default Enter and Exit Transition for AllCastActivity

public void setAnimation()
{
if(Build.VERSION.SDK_INT>20) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.LEFT);
slide.setDuration(400);
slide.setInterpolator(new DecelerateInterpolator());
getWindow().setExitTransition(slide);
getWindow().setEnterTransition(slide);
}

3) Start Activity with Intent

Write this method in `MovieDetailActivity` to start `AllCastActivity`

public void startActivity(){   Intent i = new Intent(this,AllCastActivity.class);    if(Build.VERSION.SDK_INT>20){ 
ActivityOptions options =
ActivityOptions.makeSceneTransitionAnimation(this);
startActivity(i,options.toBundle());
}else {
startActivity(i);
}
}

Most important!

put your `setAnimation()`method before `setContentView()` method otherwise the animation will not work.
So your AllCastActivity.java should look like this

class AllCastActivity extends AppcompatActivity {   @Override
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setAnimation();
setContentView(R.layout.all_cast_activity);

...............
}
//Your Slide animation
public void setAnimation(){
if(Build.VERSION.SDK_INT>20) {
Slide slide = new Slide();
slide.setSlideEdge(Gravity.LEFT);
slide.setDuration(400);
slide.setInterpolator(new DecelerateInterpolator());
getWindow().setExitTransition(slide);
getWindow().setEnterTransition(slide);
}
}
}

And you are done.

Putting up content takes a lot of time and effort 😓.
👏 clap 👏 If you think you learned something from this article.
You can find me on Stackoverflow,Github,and,on Linkedin.
Give feedback and feel free to correct mistakes.

--

--

Rohit Singh
Rohit Singh

Written by Rohit Singh

Android Developer | Arizona State University | NASA Psyche Research Aide

Responses (1)