<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9F8C8C"
tools:context=".MainActivity">
</RelativeLayout>
package com.example.counter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnstart, btnstop,btnreset;
TextView txtcounter;
int i=1;
Handler customHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnstart=findViewById(R.id.btn_start);
btnstop=findViewById(R.id.btn_stop);
btnreset=findViewById(R.id.btn_reset);
txtcounter=findViewById(R.id.textView2);
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.postDelayed(updateTimerThread,0);
}
});
btnstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customHandler.removeCallbacks(updateTimerThread);
}
});
btnreset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i=0;
txtcounter.setText(""+i);
}
});
}
private final Runnable updateTimerThread=new Runnable() {
@Override
public void run() {
txtcounter.setText(""+i);
customHandler.postDelayed(this,1000);
i++;
}
};
}