Showing Splash screen in android apps

不打扰是莪最后的温柔 提交于 2020-01-17 05:51:12

问题


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 .


回答1:


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;
    
    
    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();
    }
    

    }.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();
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/43139777/showing-splash-screen-in-android-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!