Question

I created this app and now I wanted to use a textview to show the seconds until the other activity starts but I don't know how,I created a txtview inside the countdowntimer but its never shown

Event=new String(Edt.getText().toString());
final int time = Integer.parseInt(sec.getText().toString());

Intent myInt = new Intent(MainActivity.this,Receiver.class);

myInt.putExtra("key",Event);
endingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,2,myInt,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(time*1000),pendingIntent);

new CountDownTimer(time*1000, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {
    // TODO Auto-generated method stub
        txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
    }

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

    }
};
Was it helpful?

Solution

First you need start your counter by calling start method

But be carefull, you can only change view from thread, which create this view. One way you can do it is post runnable on view:

    CountDownTimer timer = new CountDownTimer(time*1000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            txtV.post(new Runnable() {
                @Override
                public void run() {
                            txtV.setText("Activity starts"+millisUntilFinished/1000+"seconds"); // here is the txtV which isn't shown 
                }
            });
        }

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

        }
    };
    timer.start();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top