简体   繁体   中英

Showing Splash screen in android apps

Have you ever seen the page in many apps that before launching the first activity appears and somehow it is like a waiting page ?! I am beginner to android and when I click on my apps , first a blank white page appears then after 3 seconds main activity comes out . but in many apps there is a kind of customised page before the first activity that is like a progress bar or sth . how can I customise that not so beautiful blank page before starting my app?! Thanx in advance .

Technically thing you are talking about is called splash screen .

Splash screen is used to show cold start where we can make our things ready to run an application. Google advocated to use it.

There are few approaches to show splash screen.

  1. You can use CountDownTimer like this. In SplashActivity.java

     private int TIME_OUT = 3000; 

    \n\n
     CountDownTimer countDownTimer = new CountDownTimer(TIME_OUT, TIME_OUT) { @Override public void onTick(long millisUntilFinished) { // Leave this } @Override public void onFinish() { // Start your app main activity Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); // close this activity finish(); } 
    \n\n

    }.start();

  2. You can use Handler like this. In SplashActivity.java

     private static int TIME_OUT = 3000; new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); finish(); } 

    }, TIME_OUT);

  3. Using theme - correct way of doing

Add background_splash.xml file in drawable folder

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/gray"/>
    <item>
       <bitmap
          android:gravity="center"
          android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

Add these lines in styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Add these lines in AndroidManifest.xml

<activity
    android:name=".SplashActivity"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Finally add an activity called SplashActivity.java and add these code

public class SplashActivity extends AppCompatActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

You can use the Splash Screen for this Working code:

public class Mainsplash extends Activity {


     /** Duration of wait **/
    private final int SPLASH_DISPLAY_LENGTH = 3000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splashscreen);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/
        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(Mainsplash.this,MainActivity.class);
                Mainsplash.this.startActivity(mainIntent);
                Mainsplash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

}

Xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/load_src"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splashimg"
    android:gravity="center"
    android:orientation="vertical" >

  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/ll_v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
      android:layout_alignParentBottom="true"
         >


        <ProgressBar
            android:id="@+id/prg"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>
    </RelativeLayout>




</LinearLayout>

And also in Manifest file

<activity
            android:name=".Mainsplash"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

use the intent filter for that splash activity to make it launch first

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM