Update TextView Every Second
Add following code in your onCreate() method:
Thread thread = new Thread() {
@Override
public void run() {
try {
while (!thread.isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
// update TextView here!
}
});
}
} catch (InterruptedException e) {
}
}
};
thread.start();
This code starts an thread which sleeps 1000 milliseconds every round.
Update TextView every second in Android
You can use following code:
Runnable updater;
void updateTime(final String timeString) {
timer=(TextView) findViewById(R.id.timerText);
final Handler timerHandler = new Handler();
updater = new Runnable() {
@Override
public void run() {
timer.setText(timeString);
timerHandler.postDelayed(updater,1000);
}
};
timerHandler.post(updater);
}
In this line:
timerHandler.post(updater);
time will set for the first time. i.e, updater will execute. After first execution it will be posted after every 1 second time interval. It will update your TextView every one second.
You need to remove it when the activity destroys, else it will leak memory.
@Override
protected void onDestroy() {
super.onDestroy();
timerHandler.removeCallbacks(updater);
}
Hope it will help you.
Change text in TextView every second
You can use the Handler class.
private Handler handler = new Handler();
private void startCounting() {
handler.post(run);
}
private Runnable run = new Runnable() {
@Override
public void run() {
number++;
tvFragment.setText(number);
handler.postDelayed(this, 1000);
}
};
How to update this textview with the Date every second
you can update your value using Timer
class.
val timer = Timer()
timer?.scheduleAtFixedRate(object : TimerTask() {
override fun run() {
updateTimer()
}
}, 0, 1000)
private fun updateTimer() {
runOnUiThread {
resultLabel.text = "${SimpleDateFormat("MM/dd hh:mm").format(Date())}"
}
}
Here is method to stop Time.
private fun stopTimer() {
if (timer != null) {
timer?.cancel()
timer?.purge()
timer = null
}
}
Updating TextView every N seconds?
What about using a timer?
private Timer timer = new Timer();
private TimerTask timerTask;
timerTask = new TimerTask() {
@Override
public void run() {
//refresh your textview
}
};
timer.schedule(timerTask, 0, 10000);
Cancel it via timer.cancel(). In your run() method you could use runOnUiThread();
UPDATE:
I have a livescoring app, which uses this Timer to update it every 30 sec. It looks like this:
private Timer timer;
private TimerTask timerTask;
public void onPause(){
super.onPause();
timer.cancel();
}
public void onResume(){
super.onResume();
try {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
//Download file here and refresh
}
};
timer.schedule(timerTask, 30000, 30000);
} catch (IllegalStateException e){
android.util.Log.i("Damn", "resume error");
}
}
Updating a number in a TextView every second
you may use Handler
Handler h = new Handler();
final Runnable r = new Runnable() {
int count = 0;
@Override
public void run() {
count++;
textView.setText(""+count*1.8);
h.postDelayed(this, 1000); //ms
}
};
h.postDelayed(r, 1000); // one second in ms
for stopping you may use h.removeCallbacksAndMessages(null);
Update text view after every second based on my current location
You can use postDelayed
method of Handler
at the end of your getLocation()
method like this
handler.postDelayed(new Runnable() {
@Override
public void run() {
getLocation();
}
}, 1000);
Android kotlin update TextView every second
Coroutine delay is a way to do that, which is suggested in ltp's answer,
Another way is Runnable and Handler, an example function:
fun animateTextView(handler: Handler, textView: TextView, animate: Boolean = true) {
val runnable: Runnable = object : Runnable {
var randomNumber = 0
override fun run() {
randomNumber = (1..100).random()
textView.text = "$randomNumber"
handler.postDelayed(this, 500L)
}
}
// animate == false -> end text updates
if (animate) handler.postDelayed(runnable, 500L)
else handler.removeCallbacks(runnable)
}
Example use of the function:
private val textAnimaterHandler = Handler(Looper.getMainLooper())
animateTextView(textAnimaterHandler, binding.textView)
// Stop updates:
animateTextView(textAnimaterHandler, binding.textView, animate = false)
Related Topics
How to Read and Edit Android Calendar Events Using the New Android 4.0 Ice Cream Sandwich API
Getting Certificate Details from an APK
How to Make an Intent with Multiple Actions
How to Play Audio File in Android
Adding Watermark Bitmap Over Video in Android: 4.3's Mediamuxer or Ffmpeg
How to Programmatically Trigger the Touch Event in Android
Scrolling with Multiple Listviews for Android
Android Scrollview Layout Problem
Change Viewpager Animation Duration When Sliding Programmatically
Facebook Authentication Without Login Button
Difference Between a Clickable Imageview and Imagebutton
Menu Items Are Not Showing on Action Bar
Android: How to Integrate a Decoder to Multimedia Framework
Android Open Camera from Button
Performing Action After Fragment Transaction Animation Is Finished