Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splash screen in Android Application

Tags:

android

I am modifying an open source application and want to add a splash screen to it, Can some one help me in it?

When the application starts a black screen appears for 2 to 3 seconds and then the application appears.... In the code the activity main.xml is started, I have read some forums that the splash.xml file should be created and with the help of threads splash activity and main activity should be executed parallel. Is it the right approach...?

#
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        setContentView(R.layout.main);
#

Would it not be possible that I modify main.xml and put the image (splash) in main.xml so that it appears from there?

like image 956
AAB Avatar asked Nov 29 '22 17:11

AAB


1 Answers

Use class SplashScreen as under

public class Splashscreen extends Activity {

private static final int SPLASH_DISPLAY_TIME = 3000; /* 3 seconds */

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    new Handler().postDelayed(new Runnable() {

        public void run() {

            Intent mainIntent = new Intent(Splashscreen.this,
                    MainActivity.class);
            Splashscreen.this.startActivity(mainIntent);

            Splashscreen.this.finish();
            overridePendingTransition(R.anim.mainfadein,
                    R.anim.splashfadeout);
        }
    }, SPLASH_DISPLAY_TIME);
}

}

**Add mainfadein.xml & splashfadeout.xml in res->anim folder

mainfadein.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="1000">
</alpha>

splashfadeout.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="1000" >
</alpha>

and add splash.xml just Add an ImageView and set its background as screen & add image of urchoice in layout

And make Splashscreen class as Launcher and make all other class as HOME in manifest file

like image 106
Maulik J Avatar answered Dec 05 '22 04:12

Maulik J