Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a splash screen for 3 seconds on Android?

Tags:

android

I would like the splash image to begin and stay for 3 seconds, and then disappear and continue or be replaced with the rest of the layout in the main.xml.

This is my code:

Main.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    ImageView splash = (ImageView) this.findViewById(R.id.splash);

main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- margin=0px, padding=20px -->
<!--textview padding=10dp, textSize=16sp-->
<!--px=pixel, dp=density indepen sp=scale indepen fontsize preference -->
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

   <ImageView
    android:id="@+id/splash"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/splash2"/> 

  <ImageView
    android:id="@+id/myImageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bg_main"/> 

  <ImageView
    android:id="@+id/myImageView0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bar_top"/> 

<!-- 
  <TextView android:id="@+id/textItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="10dp"
    android:paddingLeft="110dp"
    android:background="#00000000"
    android:textColor="#ffffffff"
    android:textSize="22sp" 
    android:text="Find Car"
    android:enabled="false"  
  >
-->

<TabHost android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<RelativeLayout
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="3dp">
   <FrameLayout
       android:id="@android:id/tabcontent"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1" />
   <TabWidget
       android:id="@android:id/tabs"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignBottom = "@android:id/tabcontent"
       />
    </RelativeLayout>
    </TabHost>
</RelativeLayout>
like image 362
lilzz Avatar asked Jan 22 '12 02:01

lilzz


People also ask

How do I set splash screen time?

you can use Sleep method like this in your Splash Activity onCreate method: Thread timer1 = new Thread(){ @Override public void run(){ try{ sleep(4000); } catch (InterruptedException e){ e. printStackTrace(); } finally{ Intent intent = new Intent(SplashActivity. this, NextActivity.

Does Android have a splash screen?

Android Splash Screen is the first screen visible to the user when the application's launched. Splash screen is one of the most vital screens in the application since it's the user's first experience with the application.

How do you create a splash screen?

The most straightforward way to create a simple splash screen was to create a dedicated theme overriding android:windowBackground with a drawable containing branding colors or a bitmap of a logo. The theme was set as an attribute of the launcher activity in AndroidManifest. xml.


2 Answers

For Kotlin :

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        Handler().postDelayed(Runnable {
            val i = Intent(this@SplashActivity, HomeScreen::class.java)
            startActivity(i)
            finish()
        }, 3000)


    }
}
like image 197
Vinay John Avatar answered Oct 02 '22 15:10

Vinay John


Use Handler to hold the UI for some time:

public class SplashActivity extends Activity {

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

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

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                /* Create an Intent that will start the MainActivity. */
                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(mainIntent);
                finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }
}
like image 43
Ramesh R Avatar answered Oct 02 '22 14:10

Ramesh R