User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.
Realizing UI Thread:
For example, a thread action is started, and the developer wants to update the front-end element with respect to the thread elements, runOnUIThread{…} function can be used.
Below is an example of the application where UI thread is used.
Sample App in which the text in the TextView is changed every second
Initially, the application will show a Welcome message and as soon as the start button is clicked, it will show 2 messages, “love gfg” and “not gfg” alternatively at each second.
Approach:
Step 1: Add the below code in activity_main.xml. Here, add a TextView and a button to our MainActivity layout.
html
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv1" android:layout_width="210sp" android:layout_height="100sp" android:layout_centerInParent="true" android:gravity="center" android:textSize="50sp" android:text="Welcome" /> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:layout_below="@id/tv1" android:layout_centerHorizontal="true" /> </RelativeLayout> |
Step 2: Add the below code in MainActivity. Here OnClickListener is added with the button. So it is invoked whenever the user clicks the button. In the listener, an infinite loop is created in the main thread and using the UI thread the text is changed after every second.
Java
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Assigning Layout elements val tv = findViewById<TextView>(R.id.tv1) val btn = findViewById<Button>(R.id.btnStart) val msg1 = "love gfg" val msg2 = "not gfg" // Button onClick action btn.setOnClickListener { // Declaring Main Thread Thread(Runnable { while (true) { // Updating Text View at current // iteration runOnUiThread{ tv.text = msg1 } // Thread sleep for 1 sec Thread.sleep(1000) // Updating Text View at current // iteration runOnUiThread{ tv.text = msg2 } // Thread sleep for 1 sec Thread.sleep(1000) } }).start() } } } |
Note: The While loop must be declared only inside the Thread. If a Thread is declared inside a while loop, the program doesn’t work and crashes.
Sample Timer App
From the basic concept of the above code, a timer app can be designed. Below is the code for the same:
Approach:
Step 1: Add the below code in MainActivity layout. Here a button, edittext, and textview are added. The button is used to start the timmer, edittext is used to take the input from the user, and textview is used to display the time left.
html
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/btnStart" android:layout_width="100sp" android:layout_height="50sp" android:layout_centerInParent="true" android:text="Start" /> <EditText android:id="@+id/et1" android:layout_width="100sp" android:layout_height="100sp" android:layout_centerHorizontal="true" android:layout_above="@id/btnStart" android:textSize="50sp" android:inputType="number" android:gravity="center" android:background="@color/colorPrimary" android:textColor="@color/colorAccent" /> <TextView android:id="@+id/tv1" android:layout_width="100sp" android:layout_height="100sp" android:layout_centerHorizontal="true" android:layout_below="@id/btnStart" android:gravity="center" android:textSize="50sp" android:background="@color/colorPrimaryDark" android:textColor="@color/colorAccent" /> </RelativeLayout> |
Step 2: Add the below code in MainActivity class. Here we add the onClickListener with the button. As the button is clicked, runOnUiThread function is used to display the time left.
Java
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Assigning Layout elements val et = findViewById<EditText>(R.id.et1) val btn = findViewById<Button>(R.id.btnStart) val tv = findViewById<TextView>(R.id.tv1) // Button onClick action btn.setOnClickListener{ // Converting Edit Text input to String val stringTime= (et.text).toString() // Converting stringTime to Integer val intTime= Integer.parseInt(stringTime) // Declaring Main Thread Thread(Runnable { // For loop Decrement for (i in intTime downTo 0) { // Updating Text View at // current iteration runOnUiThread{ tv.text = i.toString() } // Thread sleep for 1 sec Thread.sleep(1000) } }).start() } } } |
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between User Level thread and Kernel Level thread
- Relationship between User level thread and Kernel level thread
- Android | Running your first Android app
- Thread Priority in Kotlin & Android
- Difference between Process and User Level Thread
- Comparable interface in Kotlin
- How to create project in Android Studio using Kotlin
- How to send message on WhatsApp in Android using Kotlin
- How to Add RangeSeekbar in Android Using Kotlin?
- How to add a custom styled Toast in Android using Kotlin
- User authentication using Firebase in Android
- Why Kotlin will replace Java for Android App Development
- Android EditText in Kotlin
- Android LinearLayout in Kotlin
- Android RelativeLayout in Kotlin
- Android ListView in Kotlin
- Android FrameLayout in Kotlin
- Android TableLayout in Kotlin
- Android Toast in Kotlin
- Android Fade In/Out in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

