0

I am new to android studio and i am doing a splash screen but it won't proceed to the next activity, can anyone help me:

splash.java

`public class splash extends AppCompatActivity{

private TextView tv;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    tv = (TextView) findViewById(R.id.tv);
    iv = (ImageView) findViewById(R.id.iv);

    Animation myan = AnimationUtils.loadAnimation(this, 
R.anim.mytransition);
    tv.startAnimation(myan);
    iv.startAnimation(myan);

    Thread time = new Thread(){

        public void r () {
            try {
                sleep(5000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                startActivity(new Intent(splash.this, MainActivity.class));
                finish();

            }

        }

    };
    time.start();

    }


}`

i can't figure out what's wrong but it just stays at the splash screen.

2 Answers2

2

Your thread creation itself is wrong I believe.Threads can be either created using runnable or by extending thread class

  Thread time = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                sleep(5000);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                startActivity(new Intent(splash.this, MainActivity.class));
                finish();

            }  
        }
    });
    time.start();

For more info on thread creation

https://www.javatpoint.com/creating-thread
Sharath kumar
  • 3,976
  • 1
  • 12
  • 20
1

This code works perfectly. Show your animation and then call the handler like I have done below:

public class SplashScreen extends AppCompatActivity {

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

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

    //showing splash screen for desired time
    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, Login.class);
            startActivity(i);

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

}}