1. Android Central Question's Avatar
    public class MainActivity extends AppCompatActivity {
    private int seconds=0;
    private boolean running;
    TextView textView;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    runTimer();
    }
    private void runTimer(){

    final Handler handler=new Handler();
    handler.post(new Runnable() {
    @override
    public void run() {
    int hours = seconds/3600;
    int minutes = (seconds%3600)/60;
    int secs = seconds%60;
    String time = String.format(Locale.getDefault(),
    "%d:%02d:%02d", hours, minutes, secs);
    textView.setText(time);
    if (running) {
    seconds++;
    }
    handler.postDelayed(this, 1000);
    }
    });
    }
    public void onClickStart(View view){
    running=true;
    }
    public void onClickStop(View view){
    running=false;
    }
    public void onClickReset(View view){
    running=false;
    seconds=0;
    }
    }
    //public class MainActivity extends AppCompatActivity {
    private int seconds=0;
    private boolean running;
    TextView textView;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    runTimer();
    }
    private void runTimer(){

    final Handler handler=new Handler();
    handler.post(new Runnable() {
    @override
    public void run() {
    int hours = seconds/3600;
    int minutes = (seconds%3600)/60;
    int secs = seconds%60;
    String time = String.format(Locale.getDefault(),
    "%d:%02d:%02d", hours, minutes, secs);
    textView.setText(time);
    if (running) {
    seconds++;
    }
    handler.postDelayed(this, 1000);

    }
    });

    }

    public void onClickStart(View view){
    running=true;

    }
    public void onClickStop(View view){
    running=false;

    }
    public void onClickReset(View view){
    running=false;
    seconds=0;

    }

    }
    public class MainActivity extends AppCompatActivity {
    private int seconds=0;
    private boolean running;
    TextView textView;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    runTimer();
    }
    private void runTimer(){

    final Handler handler=new Handler();
    handler.post(new Runnable() {
    @override
    public void run() {
    int hours = seconds/3600;
    int minutes = (seconds%3600)/60;
    int secs = seconds%60;
    String time = String.format(Locale.getDefault(),
    "%d:%02d:%02d", hours, minutes, secs);
    textView.setText(time);
    if (running) {
    seconds++;
    }
    handler.postDelayed(this, 1000);

    }
    });

    }

    public void onClickStart(View view){
    running=true;

    }
    public void onClickStop(View view){
    running=false;

    }
    public void onClickReset(View view){
    running=false;
    seconds=0;

    }

    }
    public class MainActivity extends AppCompatActivity {
    private int seconds=0;
    private boolean running;
    TextView textView;

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    runTimer();
    }
    private void runTimer(){

    final Handler handler=new Handler();
    handler.post(new Runnable() {
    @override
    public void run() {
    int hours = seconds/3600;
    int minutes = (seconds%3600)/60;
    int secs = seconds%60;
    String time = String.format(Locale.getDefault(),
    "%d:%02d:%02d", hours, minutes, secs);
    textView.setText(time);
    if (running) {
    seconds++;
    }
    handler.postDelayed(this, 1000);

    }
    });

    }

    public void onClickStart(View view){
    running=true;

    }
    public void onClickStop(View view){
    running=false;

    }
    public void onClickReset(View view){
    running=false;
    seconds=0;

    }

    }
    //this code works fine .But I don't understand how runtimer() method gets called when i click start,stop or reset button because I am not calling runtimer() method from onclick methods but I am calling once from onCreate() method?
    12-26-2018 07:04 AM
  2. belodion's Avatar
    To which forum would you like this to be moved, so that you can engage with others likely to understand it?
    https://r.tapatalk.com/shareLink?sha...4&share_type=t
    ManiacJoe likes this.
    12-26-2018 07:48 AM

Similar Threads

  1. P20 Pro earpiece speaker problems after in water
    By Android Central Question in forum Ask a Question
    Replies: 3
    Last Post: 03-20-2019, 10:42 PM
  2. Honor 10 - Disabling BT headset function (redial) for BT Headphones
    By Android Central Question in forum Honor V10
    Replies: 1
    Last Post: 01-03-2019, 11:59 AM
  3. Replies: 2
    Last Post: 12-26-2018, 03:03 PM
  4. Chinese letters in grey box
    By Android Central Question in forum Ask a Question
    Replies: 1
    Last Post: 12-26-2018, 06:45 AM
  5. Honor 10 - Disabling BT headset function (redial) for BT Headphones
    By Android Central Question in forum Ask a Question
    Replies: 0
    Last Post: 12-26-2018, 05:26 AM
LINK TO POST COPIED TO CLIPBOARD