Monday, 29 September 2025

How to Update a Number Every Second (Latest Android OS)

We will achieve this automatic number update in Android.  Only two files to edit: xml and Java.



First we will set an id, change the text and increase the text size in our default xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/numberTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00"
android:textSize
="50sp"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now do some changes in Java file:

1. Create an object of TextView to set our updated text referencing from the xml file.

TextView numberTextView;

2. Create an int variable to hold the updated number. If you don't assign 0, it will set 0 by default.

int number = 0;

3. Create an object of Handler which helps run code with a delay or repeatedly on the main UI thread..

Handler handler = new Handler();

4. Create an object of Runnable.
A Runnable is like a task that you tell Android to run later — either once or repeatedly. In our example, we use it to update a number every second.

Runnable runnable = new Runnable() {
@Override
public void run() {
number++;
numberTextView.setText(String.valueOf(number));
handler.postDelayed(this, 1000);
}
};

Inside the run method we do three things:

a) Increase the value of number
b) Set the value to our text view 
c) Run again after one second

handler.postDelayed(this, 1000);

This line starts the first run after 1 second. After that, the runnable calls itself again every second — so the number keeps increasing.

4. Now inside onCreate method we have to write just two lines of code:

numberTextView = findViewById(R.id.numberTV);
handler.postDelayed(runnable, 1000);

Here we are referencing the text view from our xml file and again calling the postDelayed method of Handler object. But this is called only once when the app starts.

5. At last we have to get out of this continuous execution when the app is not in use, so just call the onDestroy method and call the removeCallbacks method of the handler object.

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}


Here is the complete code:

package com.prashantsj.apps.autoupdatenumber;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView numberTextView;
int number = 0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
number++;
numberTextView.setText(String.valueOf(number));
handler.postDelayed(this, 1000);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);

numberTextView = findViewById(R.id.numberTV);
handler.postDelayed(runnable, 1000);
}

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
}





No comments:

Post a Comment

ElevenLabs For Audio Models