Instead of the timer going by milliseconds. How can I get this 30 second timer go from 1-30? Here's what I have so far. Do I have to convert the longs to integers?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
buttonStart = (Button)findViewById(R.id.start);
textCounter = (TextView)findViewById(R.id.counter);
buttonStart.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(30000, 1000);
myCountDownTimer.start();
}});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
textCounter.setText(String.valueOf(millisUntilFinished));
}
#Override
public void onFinish() {
textCounter.setText("Finished");
}
Related
I'm trying to implement a countdown widget for my application. I need help in writing the code in such a way that it starts with a button and automatically stops when the time is up which would then show a text,"Finished". Any help is greatly appreciated ! Thank You in Advance !
I have actually tried some code examples. Gradle is able to run successfully without any problems. However, when I hit the start button, nothing happens. Here are the codes that I used:
public class countdown extends AppCompatActivity {
Button buttonStart;
TextView textCounter;
MyCountDownTimer myCountDownTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.start);
textCounter = (TextView)findViewById(R.id.counter);
buttonStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(30000, 1000);
myCountDownTimer.start();
}
});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
textCounter.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
textCounter.setText("Finished!");
}
}}
Put this code in your button click listener:
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("finished!");
}
}
.start();
Refer to this link.
I have class MainActivity below, I need to cancel the counting down on method onBackPressed from other class. I also copied the CountDownTimer class below. How do I do it properly, any help? Thank you for reply.
#Override
public void onCreate(Bundle savedInstanceState) {
textViewTime = (TextView) findViewById(R.id.tv_Time);
textViewTime.setText(getString(R.string.countdown_time));
final CounterClass timer = new CounterClass(10999, 900);
timer.start();
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
// here cancel //
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
textViewTime.setText("COMPLETED");
}
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
textViewTime.setText(hms);
}
}
CountDownTimer has a cancel() method that can be called to terminate the timer.
Your MainActivity would look like this
public class MainActivity extends Activity {
private final CounterClass timer;
#Override
public void onCreate() {
timer = new CounterClass(10999, 900);
timer.start();
/* rest of the code omitted */
}
#Override
public void onBackPressed() {
super.onBackPressed();
timer.cancel()
}
}
I am writing an Android application in which I am using timer android. It is working perfectly, now I want to make it like it should vibrate on every second until the completion of the time.
My code is given below:
public class TimerActivity extends Activity {
Button btnStart, btnStop;
TextView textViewTime;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_responders_timer);
btnStop = (Button)findViewById(R.id.CanelButton);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("10");
textViewTime.setTextSize(120);
final CounterClass timer = new CounterClass(10000,1000);
textViewTime.setTextSize(120);
timer.start();
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
textViewTime.setTextSize(50);
textViewTime.setText("Completed.");
}
#SuppressLint("NewApi")
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#Override
public void onTick(long millisUntilFinished) {
textViewTime.setText(""+millisUntilFinished / 1000);
}
}
write
import android.os.Vibrator;
...
Vibrator vibrate = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for "whatever_time_u_want" milliseconds
v.vibrate(whatever_time_u_want);
give permission
<uses-permission android:name="android.permission.VIBRATE"/>
I am working on android applications. In my project I have 3 pages.
The first page consists of 1 button.
The second page is consists of the timer code.
The third page consists of again a button.
Now my requirement is when I click on the first page button the third page should open and the timer in second page should pause. Again when I click on the third page button
the second page timer should restart the time where it is stopped and should open the first page.
I am struggling to achieve this task.Guide me through it, Suggest what should have been done to do that.
Page1.java
rowTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(),Page3.class);
startActivity(myIntent);
finish();
}
});
Page2.java
public class TimeractivitybestActivity extends Activity {
EditText e1;
MyCount counter;
Long s1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1 = (EditText) findViewById(R.id.editText1);
counter = new MyCount(15000, 1000);
counter.start();
}
public void method(View v) {
switch (v.getId()) {
case R.id.button1:
counter.cancel();
break;
case R.id.button2:
counter = new MyCount(s1, 1000);
counter.start();
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
e1.setText("DONE");
}
#Override
public void onTick(long millisUntilFinished) {
s1 = millisUntilFinished;
e1.setText("left:" + millisUntilFinished / 1000);
}
}
}
Page3.java
public void gobacktopage1(View v)
{
Intent myIntent = new Intent(v.getContext(),Page1.class);
startActivity(myIntent);
finish();
}
You can always store the timeLeft which is s1 and use it again like this, Read the comments too
1) While calling timer,check if you have any stored time
Page1.java
rowTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
long time = sp.getLong("time", 0); // get saved time of times
Intent myIntent = new Intent(v.getContext(),Page3.class);
myIntent.putExtra("time", time); // send it to page2
startActivity(myIntent);
finish();
}
});
2) Use the time to start time if it's not 0.
Page2.java
public class TimeractivitybestActivity extends Activity {
EditText e1;
MyCount counter;
Long s1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
long time = this.getIntent().getLongExtra("time", 0); // get
// saved
// time
time = (time != 0) ? time : 1500;
e1 = (EditText) findViewById(R.id.editText1);
counter = new MyCount(time, 1000); // start with saved time
counter.start();
}
public void method(View v) {
switch (v.getId()) {
case R.id.button1:
counter.cancel();
break;
case R.id.button2:
counter = new MyCount(s1, 1000);
counter.start();
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
e1.setText("DONE");
}
#Override
public void onTick(long millisUntilFinished) {
s1 = millisUntilFinished;
e1.setText("left:" + millisUntilFinished / 1000);
}
}
public void onPause() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
Editor et = sp.edit();
et.putLong("time", s1); // save time SharedPreference in onPause
et.commit();
}
}
3) no change in page 3, I suppose.
Page3.java
public void gobacktopage1(View v)
{
Intent myIntent = new Intent(v.getContext(),Page1.class);
startActivity(myIntent);
finish();
}
public class TimerActivity extends Activity{
EditText e1;
MyCount counter;
Long s1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
counter= new MyCount(5000,1000);
counter.start();
}
public void asdf(View v)
{
switch(v.getId())
{
case R.id.button1: counter.cancel();
break;
case R.id.button2: counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer
{
public MyCount(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
#Override public void onFinish()
{
e1.setText("DONE");
}
#Override public void onTick(long millisUntilFinished)
{
s1=millisUntilFinished;
e1.setText("left:"+millisUntilFinished/1000);
}
}
}
this is how it works...
MyCount counter;
Long s1;
counter= new MyCount(300000,1000);
counter.start();
public void asdf(View v){ <---- method for onclick of buttons pause and resuming timer
switch(v.getId()){
case R.id.button1:<-- for pause
counter.cancel();
break;
case R.id.button2:<--- for resume
counter= new MyCount(s1,1000);
counter.start();
}
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
mediaplayer.stop();
mediaplayer.release();
}
#Override
public void onTick(long millisUntilFinished) {
s1=millisUntilFinished;
}
}
case R.id.button1:<-- for pause
counter.cancel();
this the one which is used to pause the timer and start again...
and in ur case
public void gobacktopage1(View v)
{
Intent myIntent = new Intent(v.getContext(),Page1.class);
startActivity(myIntent);
finish();
}
write a method in that add counter.cancel(); in that method and call that method...
I'm trying to start a new activity "SMS.java", if I dont respond to my timer within 30secs. After 30secs, the new ativity should be started. Can anyone help me out??? The class Timer on line 5 extends a CountDownTimer..
Here's the code:
//TimerAct.java
public class TimerAct extends Activity
{
static TextView timeDisplay;
Timer t;
int length = 30000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setText("Time left: " + length / 1000);
t = new Timer(length, 1000);
t.start();
View b1 = findViewById(R.id.abort);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
t.cancel();
finish();
}
});
}
}
//Timer.java
public class Timer extends CountDownTimer
{
public Timer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished)
{
TimerAct.timeDisplay.setText("Time left: " + millisUntilFinished / 1000);
}
public void onFinish()
{
TimerAct.timeDisplay.setText("Time over!!!");
}
}
For a timer method, better you can use with threading. It will work.
This is the example for Show Timer in android using threads. It runs the thread every second. Change the time if you want.
Timer MyTimer=new Timer();
MyTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
TimerBtn=(Button) findViewById(R.id.Timer);
TimerBtn.setText(new Date().toString());
}
});
}
}, 0, 1000);
What I do is call a method from the Activity on my CountDownTimer class, like this:
//Timer Class inside my Activity
public class Splash extends CountDownTimer{
public Splash(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
nextActivity(Login.class, true);
}
#Override
public void onTick(long millisUntilFinished) {}
}
//Method on my Activity Class
protected void nextActivity(Class<?> myClass, boolean finish) {
Intent myIntent = new Intent(this, myClass);
myIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(myIntent);
if(finish)
finish();
}