user1542984
user1542984

Reputation:

How to show splash screen using Timer in android?

Can you please tell me How to show splash screen using Timer in android.I am able to show using thread ,But thread is good choice , can you please tell me the best way to handle this ? Using thread Like this

package com.example.splash_test;

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

public class MainActivity extends Activity {
    protected int _splashTime = 5000; 

    private Thread splashTread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         splashTread = new Thread() {
             @Override
             public void run() {
                 try {                       
                     synchronized(this){
                             wait(_splashTime);
                     }

                 } catch(InterruptedException e) {} 
                 finally {
                     finish();

                     Intent i = new Intent();
                     i.setClass(MainActivity.this, SecondActivity.class);
                             startActivity(i);

                     //stop();
                 }
             }
         };

         splashTread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Upvotes: 5

Views: 16305

Answers (5)

Aditya Patil
Aditya Patil

Reputation: 1486

Kotlin Way

Timer().schedule(object : TimerTask() {
        override fun run() {
            startActivity(Intent(applicationContext, MainActivity::class.java))
        }
    }, 3000)

Upvotes: 0

Sanjay Android Ais
Sanjay Android Ais

Reputation: 19

out_marginLeft="50dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="50dp"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/txtProgress"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:progress="50"
                android:progressTint="@android:color/black" />

            <TextView
                android:id="@+id/txtProgress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Progress"
                android:textColor="@android:color/black" />
        </LinearLayout>  

Java

Upvotes: 1

Morozov
Morozov

Reputation: 5250

Also u can insert in yours onCreate:

SystemClock.sleep(TimeUnit.SECONDS.toMillis(3));

p.s. check this realisation of SplashScreen link.

Upvotes: 0

User
User

Reputation: 4211

This snippet is very helpful to design a timer in short. Try this...

new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}, 5000);

Here I'm starting MainActivity after 5 seconds. Simply put this code into onCreate() of your splash activity.

Upvotes: 5

Md. Monsur Hossain Tonmoy
Md. Monsur Hossain Tonmoy

Reputation: 11085

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;

public class SplashScreen extends Activity {

protected int _splashTime = 2000;

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

    int secondsDelayed = 1;
    new Handler().postDelayed(new Runnable() {
        public void run() {
            startActivity(new Intent(SplashScreen.this,
                    SecondActivity.class));
            finish();
        }
    }, secondsDelayed * 1000);
}

}

Upvotes: 1

Related Questions