Search code examples
androidsplash-screenrunnable

How can I show some views in return with time intervals and after the last one, open another activiy?


I want to make an Splash Screen and I want to show some views (which already made them Invisible in XML) and after the last one made Visible via Java code, I want to open another activity, I know I should make handler and runnables to do so, but I don't know exactly how to do it! can someone please show me how in code? this is what I've done so far.

public class SplashActivity extends AppCompatActivity {
private static final int SPLASH_TIME_OUT = 3600;
private static final int VIEW_COUNT = 4;
private TextView welcome_tv1, welcome_tv2, welcome_tv3;
private ImageView logo;
private Typeface typeface;
private Handler handler  = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_splash);
    init();
    setAnimations();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        }
    }, SPLASH_TIME_OUT);
}

private void setAnimations() {
    YoYo.with(Techniques.FadeInDown)
            .duration(2500)
            .repeat(0)
            .playOn(logo);
    logo.setVisibility(View.VISIBLE);
    YoYo.with(Techniques.FadeInDown)
            .repeat(0)
            .delay(400)
            .duration(1000)
            .playOn(welcome_tv1);
    welcome_tv1.setVisibility(View.VISIBLE);
    YoYo.with(Techniques.FadeInDown)
            .repeat(0)
            .delay(1400)
            .duration(1000)
            .playOn(welcome_tv2);
    welcome_tv2.setVisibility(View.VISIBLE);
    YoYo.with(Techniques.FadeInDown)
            .repeat(0)
            .delay(2800)
            .duration(800)
            .playOn(welcome_tv3);
    welcome_tv3.setVisibility(View.VISIBLE);
}

private void init() {
    welcome_tv1 = findViewById(R.id.welcome_tv_one);
    welcome_tv2 = findViewById(R.id.welcome_tv_two);
    welcome_tv3 = findViewById(R.id.welcome_tv_three);
    typeface = Typeface.createFromAsset(getApplicationContext().getAssets(),
            "Fonts/myFont.ttf");
    logo = findViewById(R.id.logo);
    setTextViews();
}

private void setTextViews() {
    welcome_tv1.setText(getResources().getString(R.string.welcome_tv_txt));
    welcome_tv2.setText(getResources().getString(R.string.welcome_tv2_txt));
    welcome_tv3.setText(getResources().getString(R.string.welcome_tv3_txt));
    welcome_tv1.setTypeface(typeface);
    welcome_tv2.setTypeface(typeface);
    welcome_tv3.setTypeface(typeface);
}

Solution

  • If you want to create welcome screens that will only run for the first time the user starts the application you can use the SharedPreferences to check if it is the first time, display your welcome views slider and after the last one open your activity.

    You can see the whole implementation here

    Hope it helps