Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How to create a timer in Android Studio?

So I have created the splash screen for my application, it displays but does not go to the homepage, (Homepage file: homepage.xml) how do I make a timer to go off and then take the user from splash to homepage in 5 secs?

This is my code:

package everingapplications.fishinglife;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Doctors on 8/4/2015.
 */
public class Splash extends Activity{

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

    }
}
package everingapplications.fishinglife;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Doctors on 8/4/2015.
 */
public class Splash extends Activity{

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

    }
}
package everingapplications.fishinglife;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Doctors on 8/4/2015.
 */
public class Splash extends Activity{

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

    }
}

1 Answer

Hi, You have to add this code to the "onCreate" of the splash screen:

package everingapplications.fishinglife;

import android.app.Activity;
import android.os.Bundle;

public class Splash extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        /* Inset This Code Here */

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(Splash.this, homepage.class);
                startActivity(intent);
                finish();
            }
        }, 5000);
    }
}

Thank you for your thoughtful reply, I got all lot of errors, I was able to fix most of them except these two, the R's highlited in red and the system cannot resolve the postDelayed same with 'R'

Thank you for your thoughtful reply, I got all lot of errors, I was able to fix most of them except these two, the R's highlited in red and the system cannot resolve the postDelayed same with 'R'

1) Check your layout ids.

2) Clean your project

3) Rebuild porject