I am working on a timer app in android studio for learning purposes. and I am looking for a way to make my code a bit more concise and tidy. For Updating the UI in my thread I used Runnable methods out side of the code block and just call them from the inside. but is there a way to use fewer lines of code to make UI commands inside the thread for something like this:
bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
running = true;
thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
getCurrentTime();
handler.post(setTV);
Thread.sleep(1000);
if (running == false) {
thread.sleep(3000);
handler.post(stopTv);
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
});
and these two are the methods:
final Runnable setTV = new Runnable() {
@Override
public void run() {
tv.setText("" + hr + ":" + min + ":" + sec);
}
};
final Runnable stopTv = new Runnable() {
@Override
public void run() {
tv.setText("Time Stopped!!!");
}
};