How to Add Fade or Any Animation to Android App - Android Studio 2.2.1 Tutorial

Animation with android apps really make sense! They are highly useful to attract more attention from users and they never get fed up with your app.

So, How to do this in your existing app ?


Here is our own tutorial with complete vocal explanation from our YouTube Channel


Manually doing this? Download the full source code and add this to your project files.




How to manually do this?

MainActivity.Java 
 package com.sabithpkcmnr.screenonapp;  

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {
private static int WELCOME_TIMEOUT = 4000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent welcome = new Intent(MainActivity.this, SecondActivity.class);
startActivity(welcome);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
finish();
}
}, WELCOME_TIMEOUT);

}
}

We have added a Intent that can open second activity or next activity after 4 seconds (4000 milliseconds) as we have given the the WELCOME_TIMEOUT


SecondActivity.Java
 package com.sabithpkcmnr.screenonapp;  

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

}
}


Nothing is done with this activity




activity_main & activity_second
We haven't added any buttons or anything in these two xml files! So it's not necessary,leave it.

Anim
We created a folder named "anim" under the "res" folder & added two xml animation files in it. Here's the "fade_in.xml" & "fade_out.xml" files.

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



fade_out.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="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />




Finally, run your app there you may find a screen and after 4 seconds it open second window with a fade in transition.


Hope that was helpful!
Thanks!

No comments:

Powered by Blogger.