Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom animations for starting activities in Android not working as expected

I'm kind of new to Android. I need to customize animations in my app when the activities open.

I used following code in my application styles.xml

<style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
    <item name="android:windowEnterAnimation">@anim/fade_in</item>
    <item name="android:windowExitAnimation">@anim/fade_out</item>
</style>

Then applied the style to a theme, in the same file.

<style name="YourTheme" parent="android:Theme.Translucent">
    <item name="android:windowAnimationStyle">@style/CustomAnimationActivity</item>
</style>

Then added the theme in my AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/YourTheme" >
</application>

But, when I'm running this, following error occurs.

errors

I think I need to add animation ml files somewhere in my project. but, no idea how to do that. Someone please help me with this.

Thanks in advance. :)

-edit-

Here is the fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0"/>
</set>

Here is the fade_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0"/>
</set>

Crash Log

05-20 15:36:47.216    3557-3557/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myayubo, PID: 3557
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.PreSplash}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
            at com.myayubo.PreSplash.onCreate(PreSplash.java:23)
            at android.app.Activity.performCreate(Activity.java:5264)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
            at dalvik.system.NativeStart.main(Native Method)
like image 517
Manuli Avatar asked May 20 '16 08:05

Manuli


People also ask

What can be used to animate between two or more views in Android?

The transitions framework can run animations between a starting and an ending scene. You can create your scenes from a layout resource file or from a group of views in your code. However, the starting scene for your transition is often determined automatically from the current UI.


2 Answers

In Android Studio:

  • right click the res folder.
  • New > Android resource directory.
  • For Resource type: select anim.
  • Press Ok, and you have the anim res folder.

You can then create/include the items for your CustomAnimationActivity as in your styles.

Edit following crash log

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.`

Just change your styles.xml to extend an AppCompat theme e.g.

<style name="YourTheme" parent="android:Theme.AppCompat.Light">
        <item name="android:windowAnimationStyle">@style/CustomAnimationActivity</item>
</style>

Also, your Activity should probably extend AppCompatActivity (or Activity).

like image 165
ade.akinyede Avatar answered Oct 15 '22 09:10

ade.akinyede


Try to add below xml files in anim folder

fadein.xml

<?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="700" />

fadeout.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="700" />  

After doing this just add below code in your splash activity (Note. Place this code after finishing your intent)

overridePendingTransition(R.anim.fadein, R.anim.fadeout);
like image 34
MashukKhan Avatar answered Oct 15 '22 08:10

MashukKhan