Android edit system files - android

I've recently started working on my first application for android, and I'm a bit stuck. My application uses the CountDownTimer class, which, it seems, malfuncions - CountDownTimer.cancel() doesn't work. I do know how to fix this by modifying the CountDownTimer.java file, but I don't have the permission to do so. I'm working in Android Studio. I couldn't find an explanation online, even though it seems quite a fundamental question, isn't it? Perhaps I'm not using the right terms?
Thank you very much for your help, and I apologize if my question's a bit ignorant haha.

Try this way :
public class Main extends Activity implements OnClickListener
{
private static final String tag = "Main";
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
private final long startTime = 50000;
private final long interval = 1000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
countDownTimer = new MalibuCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
}
#Override
public void onClick(View v)
{
if (!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESET");
}
}
// CountDownTimer class
public class MalibuCountDownTimer extends CountDownTimer
{
public MalibuCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
#Override
public void onFinish()
{
text.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
}
#Override
public void onTick(long millisUntilFinished)
{
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}

Related

Multiple timers on the same button

How can I make 4 timers of 10 mins countdown with the click of one button? All I could've done was one timer and the rest of them (3) stuck on 00:00. I know that I have to do the same thing I did to the one it's working but I made the first one a long time ago. Can someone explain me what to do and where to do it?
private static final long START_TIME_IN_MILLIS = 600000;
private TextView mTextViewCountDown;
private TextView mTextViewCountDown_2;
private TextView mTextViewCountDown_3;
private TextView mTextViewCountDown_4;
private Button mButtonStartPause;
private Button mButtonReset;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
mTextViewCountDown = findViewById(R.id.text_view_countdown);
mTextViewCountDown_2 = findViewById(R.id.text_view_countdown_2);
mTextViewCountDown_3 = findViewById(R.id.text_view_countdown_3);
mTextViewCountDown_4 = findViewById(R.id.text_view_countdown_4);
mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);
mButtonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mTimerRunning) {
pauseTimer();
} else {
startTimer();
}
}
});
mButtonReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resetTimer();
}
});
updateCountDownText();
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonStartPause.setVisibility(View.INVISIBLE);
mButtonReset.setVisibility(View.VISIBLE);
}
}.start();
mTimerRunning = true;
mButtonStartPause.setText("Pause");
mButtonReset.setVisibility(View.INVISIBLE);
}
private void pauseTimer() {
mCountDownTimer.cancel();
mTimerRunning = false;
mButtonStartPause.setText("Start");
mButtonReset.setVisibility(View.VISIBLE);
}
private void resetTimer() {
mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
mButtonReset.setVisibility(View.INVISIBLE);
mButtonStartPause.setVisibility(View.VISIBLE);
}
private void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
} ```
You basically have to make 3 more timer variables, 3 more mTimeLeftInMillis variables, start the other 3 timers, and then update the other 3 textviews the same way you have done with the first one.
So, you could do something like this (assuming you want to start/stop all 4 at the same time):
private CountDownTimer mCountDownTimer;
private CountDownTimer mCountDownTimer2;
private CountDownTimer mCountDownTimer3;
private CountDownTimer mCountDownTimer4;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
private long mTimeLeftInMillis2 = START_TIME_IN_MILLIS;
private long mTimeLeftInMillis3 = START_TIME_IN_MILLIS;
private long mTimeLeftInMillis4 = START_TIME_IN_MILLIS;
Then, in your startTimer method, you initialise the other 3 timers the same way you did the first one, using this:
mCountDownTimer2 = new CountDownTimer(mTimeLeftInMillis2, 1000) {...
mCountDownTimer3 = new CountDownTimer(mTimeLeftInMillis3, 1000) {...
mCountDownTimer4 = new CountDownTimer(mTimeLeftInMillis4, 1000) {...
The only difference in the body being the updateCountDownText method, because you will have to define 3 more methods to update each other textview to display each timer, maybe like this:
private void updateCountDownText2() {... // update mTextViewCountDown_2 in this method
private void updateCountDownText3() {... // update mTextViewCountDown_3 in this method
private void updateCountDownText4() {... // update mTextViewCountDown_4 in this method
Then use these new methods in your mCountDownTimer2, mCountDownTimer3 and mCountDownTimer4 definitions.
There are shorter ways to do it, but that's the gist of it.

Chronometer app for android

I'm a begginer in android programming and trying to improve my knowledge. I want to make an app that is a regressive chronometer, like this: I insert the seconds I want in a text view named "seconds", then press a button and the app counts down the seconds (for example: 50, 49, 48, ..., 0) in another text view called, let's say... "timeRemaining".
How this can be done? I have read some other questions here, but to be honest, I could not understand them...
Well, I managed to do something here, The countdown is working, but only if I set the time directly on the code. I cannot find a way to implement the conversion of the number (text) entered in the text view to LONG and then use this value as the time to countdown.
There is no error when debugging, but when the simulator is about to run the app and opens the activity, it stop with the message "Unfortunately, app has stopped"
Here's the code, if anyone can help me finding what I'm missing, I'll be glad!
Thank you all!
public class cronometro extends Activity implements View.OnClickListener {
Long tempo3 = Long.parseLong(findViewById(R.id.tempo).toString());
private CountDownTimer countDownTimer;
private boolean timerStarted = false;
private Button buttonStart;
public TextView textView;
private final long startTime = tempo3 * 1000;
private final long interval = 1 * 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cronometro);
buttonStart = (Button) this.findViewById(R.id.button);
buttonStart.setOnClickListener(this);
textView = (TextView) this.findViewById(R.id.textView);
countDownTimer = new CountDownTimerActivity(startTime, interval);
textView.setText(textView.getText() + String.valueOf(startTime/1000));
}
#Override
public void onClick(View v) {
if (!timerStarted) {
countDownTimer.start();
timerStarted = true;
buttonStart.setText("PARAR");
} else {
countDownTimer.cancel();
timerStarted = false;
buttonStart.setText("REINICIAR");
}
}
public class CountDownTimerActivity extends CountDownTimer {
public CountDownTimerActivity(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
textView.setText("tempo esgotado!");
}
#Override
public void onTick(long millisUntilFinished) {
textView.setText("" + millisUntilFinished/1000);
}
}
}

how to get value from countdowntimer and display into toast

i have countdown timer from 1 to 9999 if i click start button the count will start, but if click stop button i need to get current value from countdown and display that value in toast but the countdown could not stop if i click stop button please help me
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView ;
private final long startTime = 9999 * 1;
private final long interval = 1 *1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1));
}
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
/*countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");*/
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
//text.setText("Time's up!");
countDownTimer.start();
}
#Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1);
}
}
thank you
Here is my countdown timer:
QuestionCountdownTimer
public class QuestionCountdownTimer extends CountDownTimer {
private TextView remainingTimeDisplay;
private Context context;
public QuestionCountdownTimer(Context context,long millisInFuture, long countDownInterval,TextView remainingTimeDisplay) {
super(millisInFuture, countDownInterval);
this.context = context;
this.remainingTimeDisplay = remainingTimeDisplay;
}
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
remainingTimeDisplay.setText(hms);
}
#Override
public void onFinish() {
Toast.makeText(context,"COUNTDOWN FINISH :)",Toast.LENGTH_SHORT).show();
}
}
Note:
TextView remainingTimeDisplay
remainingTimeDisplay.setText(hms);
I use it to display the remaining time using a TextView
Here I call the timer:
//Start Quiz timer
QuestionCountdownTimer timer = new QuestionCountdownTimer(this,10000, 1000, remainingTimeDisplay);
timer.start();
-first parameter: this - I use it for context to show Toast message
-second parameter: 10000 - total time (10 sec)
-third parameter: 1000 - countdown interval (1 sec)
-last parameter: dispaly remaining time in real time
Tested and working
Create your CountDownTimer like this:
public class MyCountDownTimer extends CountDownTimer
{
private long timePassed = 0;
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
#Override
public void onFinish()
{
//text.setText("Time's up!");
countDownTimer.start();
}
#Override
public void onTick(long millisUntilFinished)
{
timePassed++;
text.setText("" + millisUntilFinished / 1);
}
public long getTimePassed()
{
return timePassed;
}
}
And on your onClick just do:
((MyCoundDownTimer) countDownTimer).getTimePassed();
to retrieve the time and set your textview text to it.
You should use handler
private Handler tickResponseHandler = new Handler() {
public void handleMessage(Message msg) {
int time = msg.what;
//make toast or do what you want
}
}
and pass it to MyCountDownTimer constructor
private Handler handler;
public MyCountDownTimer(long startTime, long interval, Handler handler) {
super(startTime, interval);
this.handler = handler;
}
And send message
#Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1);
Message msg = new Message();
msg.what = millisUntilFinished/1;
handler.sendMessage(msg);
}
That's all you need to do :)

CountDownTimer showing in 00:00:00 format

I have a count down timer that is meant to reading a time and stop at a particular time.
show use time and remaining time. I have codded a sample in my activity. But the function that is suppose to help me display in 00:00:00 format does not work well. It only displays it in that format when the timer as stoped.
public class PracticeQuestionActivity extends SherlockActivity implements OnClickListener {
private long timeElapsed;
private boolean timerHasStarted = false;
private final long startTime = 50000;
private final long interval = 1000;
MyCountDownTimer countdown = null;
TextView timerText = null, ElaspedTime = null;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.practicequestion);
context = PracticeQuestionActivity.this;
initializeComponents(context);
countdown = new MyCountDownTimer(startTime,interval);
this.controlTimer(); //This is going to start the timer
}
private void controlTimer(){
if (!timerHasStarted)
{
countdown.start();
timerHasStarted = true;
}
else
{
countdown.cancel();
timerHasStarted = false;
}
}
//This is going to format the time value from duration in second
private String setTimeFormatFromSeconds(long durationSeconds){
return String.format("%02d:%02d:%02d", durationSeconds / 3600, (durationSeconds % 3600) / 60, (durationSeconds % 60));
}
//This method is going to be used to initialize the components of the view
private void initializeComponents(Context context){
timerText = (TextView)findViewById(R.id.timer);
ElaspedTime = (TextView)findViewById(R.id.timeElapsed);
}
// CountDownTimer class
public class MyCountDownTimer extends CountDownTimer
{
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
#Override
public void onFinish()
{
timerText.setText("Time's up!");
ElaspedTime.setText("Time Elapsed: " + setTimeFormatFromSeconds(startTime));
}
#Override
public void onTick(long millisUntilFinished)
{
timerText.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
ElaspedTime.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}
SimpleDateFormat is your friend:
private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
#Override
public void onFinish() {
timerText.setText("Time's up!");
ElaspedTime.setText("Time Elapsed: " + timeFormat.format(startTime);
}
#Override
public void onTick(long millisUntilFinished) {
timerText.setText("Time remain:" + timeFormat.format(millisUntilFinished));
timeElapsed = startTime - millisUntilFinished;
ElaspedTime.setText("Time Elapsed: " + timeFormat.format(timeElapsed));
}

Count down from user-defined amount of time

I have a working countdown, but it only counts down from a hard coded integer that I put in. I want the user to be able to type in a number and have it count down from that number instead. I want the text put into "timeedit" to be put into a string and put to the value of "startTime".
EDIT: If the code below is not indented correctly on your screen, you can also view the code here: http://pastebin.com/BnzEtFX5
Code:
public class TimerActivity extends Activity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
public String time;
private long startTime = 30 * 1000;
private final long interval = 1 * 1000;
private EditText timeedit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeedit = (EditText) findViewById(R.id.timeedit);
countDownTimer = new MyCountDownTimer(startTime, interval);
time = timeedit.getText().toString();
text.setText(time); //+ String.valueOf(startTime/1000)
}
#Override
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startTime = 30 * 1000;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
#Override
public void onFinish() {
text.setText("Time's up!");
}
#Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1000);
}
}
}
Ummm...I'm not sure what the problem is but if I understand you correctly, you just need to do something like this in onCreate()
String userTime = timeedit.getText().toString();
startTime = Long.parseLong(userTime);
Edit
you will actually want to put startTime and userTime in your onClick. If it is in onCreate() then your EditText will be empty unless you put a default value in your xml but either way it won't be what the user has entered. You also need to surround with a try/catch in case the user enters something other than numeric characters.
public void onClick(View v) {
String userTime = timeedit.getText().toString();
long startTime = Long.parseLong(userTime);
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP")
If You want to let user do some input, you simply have to get this with:
String userCountDown = timeEdit.getText().toString();
then parse it into Long:
long userStartTime = Long.parseLong(userCountDown);
and then, pass it to the countDown
countDownTimer = new MyCountDownTimer(UserStartTime, interval);
But You have to be sure that the value inside your timeEdit is an Long value. By setting the attribute inputType="number" inside Your layout.xml file to the R.id.timeedit, you can get sure to get an Long. But to be very sure that there is an input, You can do any check and show a warning for the user.
if (!timerHasStarted) {
String userCountDown = timeEdit.getText().toString();
if(userCountDown.length()<1){
Toast.makeText(yourActivity.this,"PLEASE DO SOME INPUT",Toast.LENGTH_LONG).show();
}else{
long userStartTime = Long.parseLong(userCountDown);
countDownTimer = new MyCountDownTimer(userStartTime, interval);
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
}
} else {
countDownTimer.cancel();
timerHasStarted = false;
startTime = 30*1000;
startB.setText("RESTART");
}

Categories

Resources