Search code examples
javaandroidandroid-alertdialogsplash-screen

Splash screen depends onClick to move on


Currently, my splash screen directly link to my mainActivity. The splash screen ends when the rotate duration finish. android:duration="8000"

Then I have alert dialog at the end there.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);


        final ImageView iv = (ImageView) findViewById(R.id.imageView);
        final Animation an = AnimationUtils.loadAnimation(getBaseContext(), R.animator.rotate);
        final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);

        iv.startAnimation(an);
        an.setAnimationListener(new Animation.AnimationListener() {


            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv.startAnimation(an2);
                finish();
                Intent i = new Intent(getBaseContext(), MainActivity.class);
                startActivity(i);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        new AlertDialog.Builder(this).setTitle("Promo Code").setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U").setNeutralButton("Close", null).show();

    }
`

So, is there any way to like end the splash screen after I click on close ?

This may really sounds stupid cause I'm really new in android


Solution

  • AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(yoursplashscreen.this);
    
           dialogBuilder.setTitle("Promo Code");
    dialogBuilder.setMessage("Type in 'VC2001' for a 10% discount. Only applicable for VC-20U");
    
    
            dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    // finish your splash screen here and set intent to your new activity
                    finish();
                    Intent i = new Intent(getBaseContext(), MainActivity.class);
                    startActivity(i);
                }
            });
            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();