-1

Hello I am new to android application development and I want to add a splash screen in my application like one that appears in iPhone applications that is only for the duration while my application is loading up.

In most places I found the code that adds a separate screen for splash and displays it to 1-5 seconds.

In my opinion this is not the actual concept of splash screen. So can anyone tell me how to add splash screen in android application.

parsley72
  • 8,449
  • 8
  • 65
  • 98
improgrammer
  • 552
  • 6
  • 7
  • 3
    What is "the actual concept" in your opinion? The samples you found would probably be the way and just make the time whatever you want. However, splash screens are typically frowned upon in Android applications – codeMagic Aug 08 '14 at 19:33
  • @codeMagic actually they make sense if you need to load tons of data at startup – Droidman Aug 08 '14 at 19:35
  • But I think that the splash screen should only be displayed for the period of loading not for specific time period. Can I do that in andriod? – improgrammer Aug 08 '14 at 19:35
  • @Droidman rarely are they needed. We have background tasks and progress bars/dialogs – codeMagic Aug 08 '14 at 19:36
  • what i have done is that i have created a new activity, showed it for 5 seconds, then finish That activity and started a new activity – Jamil Aug 08 '14 at 19:36
  • I want to add a splash, but I have only little data to load. I takes small time interval, but I don't want the black screen. – improgrammer Aug 08 '14 at 19:37
  • I've also done what SoftCoder has done. – Kristy Welsh Aug 08 '14 at 19:38
  • But this is not accurate, in iPhone app time of splash screen depends on time of loading the app. It is not for prespecified time. – improgrammer Aug 08 '14 at 19:39
  • @improgrammer yeah, you load the data on a background thread like in an `AsyncTask` of the splash screen activity then call `finish()` in `onPostExecute()` and start your first "real" activity – codeMagic Aug 08 '14 at 19:41
  • [See this answer on using an AsyncTask](http://stackoverflow.com/questions/18898039/using-asynctask/18898105#18898105). Just make your splash a separate activity (your launcher activity) – codeMagic Aug 08 '14 at 19:43

6 Answers6

1

Create SplashScreen which extends Activity and then write this code block inside it

Warning:MainActivity is your base Activity which is default activity when you create a new Project

public class SplashScreen extends Activity {


public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
Thread splashThread = new Thread() {
@Override
public void run() {
try {
int waited = 0;
while (waited < 2000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
// do nothing
} finally {
Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);
finish();
}
}
};
splashThread.start();
}
}

By the way,if you are new to android development please learn Thread Topic before implement SplashScreen

Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28
0

In your activity, while you are loading the data, you can add a fragment which will show whichever progress you want. Once the activity is done with loading, you can remove that fragment.

Other option, use a widget like https://github.com/marvinlabs/android-progress-panel that allows you to switch views.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124
  • I want splash screen not a progress panel – improgrammer Aug 08 '14 at 19:38
  • @MarvinLabs linked android-progress-panel in an effort to give you a code reference base, not trying to change your intention to a progres panel. Use android-progress-panel's available source code to create your own splash screen. – IAmTheSquidward Aug 08 '14 at 19:47
0

For splash screen, just create layout with simple image (If you don't need fancy with animation)

splash_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/image" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="12dp"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:text="Some Text Here" />        
</RelativeLayout>

Inside your SplashScreenActivity.java class, replace the onCreate method with below code.

//Time for which you want to show splash screen
private static int SPLASH_SCREEN_TIME_OUT = 200;  

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

       new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
                startActivity(i);               
                finish();
            }
        }, SPLASH_SCREEN_TIME_OUT);
    }
AnkitSomani
  • 1,152
  • 8
  • 9
0

try this

 package com.sunil.assignment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

public class Splash extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashh);
        final Fragment spl=new Fragment();
    FragmentTransaction ft= getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment1, spl);
    ft.commit();



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

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //Intent intent=new Intent(Splash.this,MainActivity1.class);
               // Splash.this.startActivity(intent);

                //Splash.this.finish();  

                findViewById(R.id.fragment1).setVisibility(View.GONE);
                Home h=new Home();
                FragmentTransaction ft=getFragmentManager().beginTransaction();
                //ft.hide(spl);
                ft.replace(R.id.rel1, h);
                ft.commit();
            }
        }, 2000);   
    }

}

splashh.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rel1" >

    <fragment
        android:id="@+id/fragment1"
        android:name="com.sunil.assignment.Spl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
      />  

</RelativeLayout>
sunil
  • 660
  • 8
  • 20
0

Show your splash screen and in background load your data. When you have loaded everything that you need, then notify your activity for that. When your activity receives that notification, just start your next (main) activity.

dragi
  • 3,385
  • 5
  • 22
  • 40
0
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // code u want to run after 5 second
        //startactivity(new Intent(Splash.this,MainScreen.class))
        //finish();
    }
},5000);
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31