Регистрация / Вход в аккаунт
Заказать рекламу на МирМастера.ру
Блог о рыбалки и снастях

Форум » Гаместрой » Android Studio » Android Splash Screen Example: (Английский)

1. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:36)
Many popular Android Apps such as Skype, Facebook, Adobe Reader, 500px, Dropbox etc., uses splash screen to display their logo. Most Android Apps uses Android Splash Screen before launching application Activity. Android splash screen is used to display a logo or brand for an app. In this article we are going to discuss about implementing an Android Splash Screen in a simple manner.

First we will create a new default project using these simple steps:

1 Click on File > New Project.
2 Next, define Application Name and Minimum SDK and hit Next
3 Select Blank Activity and Hit Next.
4 Hit Finish.

This creates a simple Hello world Project for which we will implement android Splash screen. We already discussed in detail about Creating New Android Project in our previous article.
2. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:39)
Creating Android Splash Screen:

To display a splash screen, we need a layout first of all. So let’s create a Layout by Right Clicking on Layout (located in App > Res > Layout) and selecting New > Layout Resource File. Give it any name. I gave it splash.xml. Now select Text tab located at bottom as shown in image below.
Images
3. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:40)
Now Open File Explorer in your Operating System and paste the image you want to use as splash screen in Drawable Folder. (Generally located at C:\Users\UserName\AndroidStudioProjects\AndroidSplashScreenExample\app\src\main\res\drawable) . Advisable Image size is 800 x 1280 pixels and format is jpeg or png. Make sure that you named the image using small letters and underscores without using Special Symbols. I named it as splash_image.
4. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:44)
Now type the following code in Text Tab of splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_image"
android:orientation="vertical">

</LinearLayout>


Background is set to splash_image we just copied in Drawable folder. Here layout’s width and height are set to match_parent so that the background image fills the entire Layout.
5. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:48)
Splash screen is a separate activity which will be displayed first ahead of all other Activity. We create splash screen activity by creating a class. We create a new class by Selecting App > Java > Your Package Name and right Clicking on your package and selecting New > Java Class. Give it any name. Here, I gave it as SplashScreen. Now Type the Following Code in SplashScreen activity:
package com.coderefer.androidsplashscreenexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
* Created by vamsikrishna on 12-Feb-15.
*/
public class SplashScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);

Thread timerThread = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent intent = new Intent(SplashScreen.this,MainActivity.class);
startActivity(intent);
}
}
};
timerThread.start();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

}


We use methods of Thread class such as –

sleep(long time) – to make the thread sleep for some time (in milliseconds).

start() – to start the thread.

sleep takes the time in milliseconds as its parameter(3000 => 3 seconds). Here we used this delay time as the time to display the splash screen activity. After this delay time, MainActivity is started by the code written in finally{} block.

The splash screen activity must not be shown when the user presses the back button. In order to do this, we should destroy the splash screen activity after it is shown for few seconds. This is done by the use of onPause() method. The onPause() method is a method of Activity class which comes into play when the user leaves the activity.

Next, We need to define which activity to open after displaying Splash screen. This is done using the Intent(Context, Class) constructor of the Intent class.
6. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:49)
Now to make your splash screen to work, you need to refer the SplashScreen Activity in Android Manifest. So, open the Manifest file by clicking on App > Manifests > AndroidManifest.xml. Now we add reference to new Activity we just created and Change the Launcher Activity (Activity which Launches first) to splash.xml by changing the code shown below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coderefer.androidsplashscreenexample" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

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

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

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />

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

</manifest>

7. avatar CoMMoN Создатель [автор] (13.08.2016 в 12:49)
Now Hit Run Button (or Shift+ F10) and select Emulator or Real Device to run the program. Now you can see Android Splash Screen displaying before your activity.

Вы не авторизованы, чтобы добавить сообщение, необходимо!

© Development by MirMastera.Ru 2009 - 2018

Яндекс.Метрика
Главная Правила FAQ FPORTAL