Displaying activity with custom animation - android

I have a widget which starts an activity when it is clicked. I'd like to have some kind of fancy animation to display this activity, rather than the standard scroll-from-right of Android. I'm having problems setting it, though. This is what I have:
slide_top_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/accelerate_interpolator">
<translate android:fromYDelta="-100%" android:toXDelta="0" android:duration="100" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="50" />
</set>
...which is referenced in anim.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="50%"
android:animation="#anim/slide_top_to_bottom" />
But then where do I reference it from? I've tried both the base element of the activity I want to slide in, and the activitiy's entry in the manifest, both times with
android:layoutAnimation="#+anim/anim"
I might be doing this all wrong. Any help is much appreciated!

You can create a custom Theme with a reference to your own animation and apply it to your Activity in your manifest file.
I was successful in applying a custom animation for a floating window using the following style definition. You might be able to do something similar if you set the parent of your style to be "#android:style/Animation.Activity"
Look at the following files for further details on what you can override.
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
Here's my a portion of my styles.xml and manifest.xml
styles.xml
<style name="MyTheme" parent="#android:style/Theme.Panel">
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<!-- Animations -->
<style name="MyAnimation" />
<!-- Animations for a non-full-screen window or activity. -->
<style name="MyAnimation.Window" parent="#android:style/Animation.Dialog">
<item name="android:windowEnterAnimation">#anim/grow_from_middle</item>
<item name="android:windowExitAnimation">#anim/shrink_to_middle</item>
</style>
Manifest.xml
<activity
android:name="com.me.activity.MyActivity"
android:label="#string/display_name"
android:theme="#style/MyTheme">
</activity>

startActivity(intent);
overridePendingTransition(R.anim.slide_top_to_bottom, R.anim.hold);
Check this link: overridePendingTransition method
Edit:
To Achieve the Animation for the Views. You have use the startAnimation Method like below
view.startAnimation(AnimationUtils.loadAnimation(
WidgetActivity.this,R.anim.slide_top_to_bottom));
Check this link:

It doesn't matter that your starting from a widget, wrote a tutorial so that you can animate your activity's in and out. This animation is set within the activity that your bringing into focus so you can do it with pendingIntent as well.
Enjoy:
http://blog.blundellapps.co.uk/animate-an-activity/

Related

specified duration for translate (in anim resource file) not working while starting second Activity

first of all I am using API 28 (Android 9.0)
I want my second activity to appear from left to right when I click the menu button on mainActivity.
I have overrided the pending transition in main activity like below:
startActivity(myintent);
this.overridePendingTransition(R.anim.left_to_right, R.anim.no_move);
the left_to_right.xml with duration 100ms(that doesnt seems to be 100ms, its almost 1 second, but animation works fine):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="100"
android:fromYDelta="0%"
android:fromXDelta="-100%"
android:toXDelta="0%"
android:toYDelta="0%">
</translate>
</set>
and no_move.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%"
android:toYDelta="0%"
android:fromXDelta="0%"
android:toXDelta="0%">
</translate>
</set>
this is my theme.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Chess" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="android:windowActivityTransitions">true</item>
<item name="colorPrimary">#color/purple_500</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
the activity starts how I want(animation) but the only problem is its duration.it takes like 1 second for compelete translate.
there is no extra code to mention here, all i add was that 2 anim file and overriding pendingTranstition
plus setting windowActivityTranstition to True in themes.xml
WHAT I WANT TO DO...
the animation I want here is like when you click the menu button(on the top and left) in Telegram app, the menu shows up smoothly, I want something beautifule like that.
so if there is a better way to do such things or if telegram uses another way for this beautiful animation please let me know.thanks in advance.
Use one activity with DrawerLayout view for that left menu. It has open/close functions, fade effect etc. Telegram app uses their own custom view for that menu here it is. You can also check source code of their activity here (check DrawerLayoutAdapter, DrawerLayoutAdapter variables)

Screen flickers after Google Sign In dialog dismiss android studio

I'm facing this issue and am not able to find a solution to it. I have Google Sign In implemented in my app using firebase. The problem I'm facing is that whenever the Sign In dialog dismisses a black strip runs across the screen from top to bottom. It moves very fast but still is noticeable. I want to remove this black strip that runs across the screen so the user smoothly returns to the screen.
I tried to add
overridePendingTransition(0, 0);
in onPause() and onResume() methods but still found no success.
Could anybody please help me to find a way around this and/or how could I achieve it?
I had the same problem and it bothered me, it didn't look good.
I found a solution! You can define activity open and close animations in styles.xml and assign them to the activity which starts the google sign in by using android:windowAnimationStyle. Here is an example using fade animations:
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowAnimationStyle">#style/ActivityAnimations</item>
</style>
<style name="ActivityAnimations" parent="#android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">#anim/fade_in</item>
<item name="android:activityOpenExitAnimation">#anim/fade_out</item>
<item name="android:activityCloseEnterAnimation">#anim/fade_in</item>
<item name="android:activityCloseExitAnimation">#anim/fade_out</item>
</style>
The style AppTheme is assigned to the application or the activity that launches the google sign in flow in the AndroidManifest.xml using android:theme="#style/AppTheme".
FadeIn:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500"
/>
FadeOut:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500"
/>
To the Google Sign In Intent add a flag:
signInIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
After calling startActivityForResult(signInIntent, REQ_CODE) add:
activity.overridePendingTransition(0, 0);

Activity changed animation with SingleTask and ClearTask intent

I have LoginActivity and MainActivity in my application.
I need to remove LoginActivity from backstack when user successfully logs in, MainActivity when user logouts.
Therefore I have added ClearTask and SingleTask intent flags for MainActivity and LoginActivity.
That works as expected but it has raised another problem - I lost custom animations registered with overridePendingTransitions(int, int).
Is there any way to setup custom animations with SingleTask and ClearTask intent?
Is it possible to achieve activity workflow I have described before with custom animations?
When you navigate user from LoginActivty to MainActivity you just have to call finish(); after your intent activity navigation code. You can add activity animation in main theme style.
In you main application theme. Add following :
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowAnimationStyle">#style/OutdoorTheme.Window</item>
</style>
<style name="OutdoorTheme.Window" parent="#android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">#anim/abc_fade_in</item>
<item name="android:activityOpenExitAnimation">#anim/abc_fade_out</item>
<item name="android:activityCloseEnterAnimation">#anim/abc_fade_in</item>
<item name="android:activityCloseExitAnimation">#anim/abc_fade_out</item>
</style>
abc_fade_in.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
abc_fade_out.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="#android:integer/config_mediumAnimTime" />

What is the default transition between activities in Android 4.0 (API 14+)

I built an application with quite a few activities and I would like to have "slide from right on enter/slide from left on exit" transitions between them.
I read more than once that slide transitions should be the Android default, but on the device I am developing on the transitions are fade in/fade out by default (Galaxy Tab 2 7", on ICS 4.0).
Is there anything I need to declare at application level, for example in the manifest file?
I am asking because otherwise I would need to add overridePendingTransition (R.anim.right_slide_in, R.anim.left_slide_out); to all my transitions which are plenty...just wondering if I am missing something before going that road.
Many thanks
No answers...on the devices 4+ I tried, the animation is a fade-in fade-out with zoom in or out...
I added the code manually where I wanted to have the slide animation:
//open activity
startActivity(new Intent(this, MyActivity.class));
overridePendingTransition(R.anim.right_slide_in, R.anim.left_slide_out);
xml animation right to left:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
xml animation left to right:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
In your style.xml file put
<style name="WindowAnimationTransition">
<item name="android:windowEnterAnimation">#android:anim/slide_in_left</item>
<item name="android:windowExitAnimation">#android:anim/slide_out_right</item>
</style>
and add
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
...
<item name="android:windowAnimationStyle">#style/WindowAnimationTransition</item>
</style>
There is a way which involves creating a style and then adding it in manifest here:
How to change all the activity transitions at once in Android application?,
yet I only managed to make it work over startActivity(). Whenever back is pressed this method won't work or I might just missing something.

Dialog animation fillAfter doesn't work

I can't manage to get the fillAfter property of an animation to work.
I used it both in the translate and the set of the animation in xml but it always jumps to the standard dialog position at the end. Is this even possible?
Current slide in animation in xml:
<set
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="70%"
android:fromYDelta="100%"
android:toYDelta="70%"
android:duration="2000" />
</set>
The Themes.xml that is being applied to the dialogs:
<style name="theme_dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/style_slide_in_dialog</item>
</style>
<style name="style_slide_in_dialog">
<item name="android:windowEnterAnimation">#anim/animation_slide_in_dialog</item>
<item name="android:windowExitAnimation">#anim/animation_slide_out_dialog</item>
</style>
Animations like TranslateAnimation are temporary. If you want the effect to persist when the animation is complete, you need to register an AnimationListener and do something to make the change permanent in onAnimationEnd(). For example, with a TranslateAnimation, you would need to adjust your LayoutParams to have it reside in your final position.

Categories

Resources