Activity changed animation with SingleTask and ClearTask intent - android

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" />

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 transition black screen

So I've got WelcomeActivity -> HomeActivity and closing WelcomeActivity with finish()/supportFinishAfterTransition(). I want to do either a slideTransition or a fadeTransition (open to other suggestions btw).
I've researched this and as it turns out there are 2+ ways of doing it: either with overridePendingTransition which uses anim.xml files or with Transitions (from the android docs) which use transition.xml files...
I've tried both and both give me unwanted results:
for anims: I get this ugly mid transition black screen:
fade_in.xml:
<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="300" />
fade_out.xml:
<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:zAdjustment="top"
android:duration="300" />
WelcomeActivity: (I've tried having finish before the overridePendingTransaction)
startActivity(Intent(this, HomeActivity::class.java))
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
finish()
for transitions: I can't make it so WelcomeActivity closes properly: It either closes before the animation starts or not closing at all. I'm following the android docs.. I've also tried this:
style.xml
<item name="android:windowActivityTransitions">true</item>
<item name="android:windowEnterTransition">#transition/enter_fade</item>
<item name="android:windowExitTransition">#transition/exit_fade</item>
My other questions is which approach should I have? Is Google pushing the transitions over the anims for starting new activities?
What I always do is to start an activity(any way you want, ways are listed here).
I use slide transitions using these two files:
slide_out_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
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_mediumAnimTime"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
Then I start an activity like this(this is java):
startActivity(MainActivity.this, SecondActivity.class);
overridePendingTransition(R.anim.slide_in_right.xml, R.anim.slide_in_left.xml);
finish();
Using this, the activity exits giving way to the new one smoothly from right to left.
For the black screen, set the theme of that activity as translucent in the AndroidManifest.xml file
android:theme="#android:style/Theme.Translucent"
so your code will be something like this
<activity android:name=".Activity"
android:theme="#android:style/Theme.Translucent" />
Answer for the black screen taken from: https://stackoverflow.com/a/6468734/9819031

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.

Displaying activity with custom animation

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/

Categories

Resources