In order to bring in some uniform user flow in the Open Event Android there was a need to implement slide animation for various screens. It turned out that the settings page didn’t adhere to such a rule. In this blog post I’ll be highlighting how this was implemented in the app.
Android Animations
Animations for views/layouts in android were built in order to have elements of the app rely on real life motion and physics. These are mainly used to choreograph motion among various elements within a page or multiple pages. Here we would be implementing a simple slide_in and a slide_out animation for the settings page. Below I highlight how to write a simple animator.
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="@android:integer/config_shortAnimTime" android:fromXDelta="100%" android:toXDelta="0%" /> </set>
This xml file maily is what does the animation for us in the app. We just need to provide the translate element with the position fromXDelta from where the view would move to another location that would be toXDelta.
@android:integer/config_shortAnimTime is a default int variable for animation durations.
Here toXDelta=”0%” signifies that initial position of the page i.e the settings page that is the screen itself. And the 100% in fromXDelta signifies the virtual screen space to the right of the actual screen(OUTSIDE THE SCREEN OF THE PHONE).
Using this animation it appears that the screen is sliding into the view of the user from the right.
We will be using overridePendingTransition to override the default enter and exit animations for the activities which is fade_in and fade_out as of Android Lollipop.
The two integers you provide for overridePendingTransition(int enterAnim, int exitAnim) correspond to the two animations – removing the old Activity and adding the new one.
We have already defined the enterAnim here that is slide_in_left. For the exit animation we would define another animation that would be stay_in_place.xml that would have both fromXDelta and toXDelta as 0% as their values. This was done to just avoid any exit animation.
We need to add this line after the onCreate call within the SettingActivity.
@Override protected void onCreate(Bundle savedInstanceState) { //Some Code super.onCreate(savedInstanceState); overridePendingTransition(R.anim.slide_in_right, R.anim.stay_in_place); //Some Code
Now we are done.
We can see below that the settings page animates with the prescribed animations below.
Resources
- View Animation : https://developer.android.com/guide/topics/graphics/view-animation.html
- Override Pending Transition : https://www.codota.com/android/methods/android.app.Activity/overridePendingTransition
- Using Animations in Android : http://www.vogella.com/tutorials/AndroidAnimation/article.html