-1

I was trying with a timer when the timer ends i have to show a popup box but it is not working the activity goes back to the first activity where it called is there anyway to do this? i have also tried with alert box it also not working

 Timer t = new Timer();
        t.schedule(new TimerTask() {

            @Override
            public void run() {
                //alrt();
                final Dialog myDialog1 = new Dialog(getApplicationContext());
                myDialog1.setContentView(R.layout.register);
                myDialog1.setCancelable(false);
myDialog1.show();
                // If you want to call Activity then call from here for 5 seconds it automatically call and your image disappear....
            }
        }, 10000);
    }

layout of this acticity

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="appsmash.keralapschelper.questionlist">


  <include layout="@layout/content_main"
      android:layout_marginLeft="0dp"
      app:layout_constraintLeft_toLeftOf="parent"
      android:id="@+id/include"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="parent" />

  <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      app:layout_constraintBottom_toBottomOf="parent"
      android:layout_marginBottom="8dp"
      app:layout_constraintTop_toTopOf="parent"
      android:layout_marginTop="8dp"
      android:layout_marginLeft="310dp"
      app:layout_constraintLeft_toLeftOf="@+id/include"
      app:layout_constraintVertical_bias="0.039" />
</android.support.constraint.ConstraintLayout>
chriscka
  • 127
  • 1
  • 12

2 Answers2

0

Use

new CountDownTimer(10000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        final Dialog myDialog1 = new Dialog(getApplicationContext());
        myDialog1.setContentView(R.layout.register);
        myDialog1.setCancelable(false);
        myDialog1.show();
    }
}.start();
Martin De Simone
  • 2,030
  • 3
  • 14
  • 30
0

The solution is On class

AlertDialog alert;

And Oncreate()

 final AlertDialog.Builder dialog = new AlertDialog.Builder(this)
                .setTitle("Time is over").setMessage(
                        "Your score is"+test);
        dialog.setPositiveButton("Confirm",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
alert.dismiss();

                    }
                });
        alert = dialog.create();

and outside

new CountDownTimer(30000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                tim.setText("seconds remaining: " + millisUntilFinished / 1000);
                //here you can have your logic to set text to edittext
            }


            @Override
            public void onFinish() {
                // TODO Auto-generated method stub

                alert.show();
            }
        }.start();

    }
chriscka
  • 127
  • 1
  • 12