简体   繁体   中英

making activity_main.xml as splash screen

im new in android. what if I want to make current view which I have made yet, of my app as splash screen for 5 seconds. is it possible or not ?

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background">

<ImageView
    android:id="@+id/icon"
    android:layout_width="fill_parent"
    android:layout_height="150dp"


    android:layout_centerInParent="true"
    android:contentDescription="TODO"
    android:src="@mipmap/app_icon_app_store"/>

<TextView
    android:id="@+id/firstLine"
    android:layout_width="300dp"
    android:layout_height="60dp"
    android:text="EXPRESSIONS"
    android:layout_centerHorizontal="true"
    android:textColor="#FFFFFF"
    android:textSize="50sp"
    android:layout_below="@+id/icon"
    android:gravity="center" />

    </RelativeLayout>

Create a splash activity and you can set above layout using setContentView(R.layout.activity_main);

public class SplashActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // this is count down timer for 5 seconds
    // 5000 milliseconds value is for 5 seconds display, 1000 milliseconds value is clock tick interval 
    new CountDownTimer(5000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
           // Launch whatever activity screen you want to display when done with count down timer (i.e. 5 seconds in your case)

            Intent intent = new Intent(SplashActivity.this,
                    YouNextActivity.class);//
            startActivity(intent);
            finish();

        }
    }.start();

}

}

you can do like this...

public class SplashScreenActivity extends Activity {
    // Splash screen timer
    private static int SPLASH_TIME_OUT = 5000;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /****** Create Thread that will sleep for 5 seconds *************/
        Thread background = new Thread() {
            public void run() {
                try {
                    // Thread will sleep for 5 seconds
                    sleep(SPLASH_TIME_OUT);
                       Intent i = new Intent(MainActivity.this, AnotherActivity.class);
                        startActivity(i);
                        //Remove activity
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        };
        // start thread
        background.start();
    }
}

Hope this will help you .

public class SplashActivity extends AppCompatActivity {

    private final int SPLASH_DISPLAY_LENGTH = 5000;

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

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                /* Create an Intent that will start the Menu-Activity. */
                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}

This is SplashActivity you just set your current view in setContentView()

public class SplashScreen extends AppCompatActivity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

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

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

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, YourActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}

You need to make this activity to sleep for some seconds and recall start activity function after the time:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread splashTimer = new Thread() {
            public void run() {
                try {
                    sleep(3000);

                } catch (Exception e) {
                    Log.e("error", e.toString());
                } finally {
                    startMain();
                }
            }
        };
        splashTimer.start();
    }


    public void startMain(){
        Intent iMain = new Intent(this, MainActivity.class);
        startActivity(iMain);
        finish();
    }

after startActivity() you need to call finish() to prevent this splash activity if back button pressed.

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