I'm learning Android and java while building a timer application for myself.
Referencing an old thread, Android - Controlling a task with Timer and TimerTask?
I am trying to create the Runnable method to count down my timer.
The basic java issue I'm stuck on is what to class do I attach the postDelayed() call?
My activity is called TimerButtons for now, and I thought this would work:
package com.TimerButtons;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TimerButtons extends Activity {
private TextView mDisplayTime;
private Button mButtonStart;
private Button mButtonStop;
private int timeTenths = 0;
private Drawable d;
private PorterDuffColorFilter filter;
// capture our View elements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filter = new PorterDuffColorFilter(Color.DKGRAY, PorterDuff.Mode.SRC_ATOP);
d = findViewById(R.id.buttonStart).getBackground(); d.setColorFilter(filter);
d = findViewById(R.id.buttonStop).getBackground(); d.setColorFilter(filter);
mDisplayTime = (TextView) findViewById(R.id.displayTime);
mButtonStart = (Button) findViewById(R.id.buttonStart);
mButtonStop = (Button) findViewById(R.id.buttonStop);
// add click listeners to the buttons
mButtonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Thread(r).start();
}
});
mButtonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
timeTenths = 0;
updateDisplay();
updateSetting();
}
});
// display the current time
updateDisplay();
}
// runtime methods below here
// updates the time we display in the TextView
private void updateDisplay() {
mDisplayTime.setText(
String.valueOf(((float)timeTenths)/10)
);
}
private void updateSetting() {
mTensDigit.setText(String.valueOf(timeTenths/100));
mOnesDigit.setText(String.valueOf((timeTenths%100)/10));
mTenthsDigit.setText(String.valueOf(timeTenths%10));
}
Runnable r = new Runnable()
{
public void run()
{
if (timeTenths >= 1)
{
timeTenths -= 1;
if (timeTenths != 0)
mDisplayTime.postDelayed(this, 100);
updateDisplay();
}
}
};
}
I get the error: The method postDelayed(new Runnable(){}, int) is undefined for the type TimerButtons on the commented line.
Thanks for any noob guidance!
Dave