Header Ads

Fade in Animation

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. Most likely they are used in Splash Screen and pop up windows. Today, I will show how you can introduce this feature into your app by just spending 5 minutes on Android Studio!

Getting started in pretty easy, just open your Android Studio project and follow below steps. There you can find both text and video tutorial, follow as which one is your preferred mode of learning.





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



How to manually do this?
Below are the complete codes you required in-order to get the things done.

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.


Thanks!

No comments

Powered by Blogger.