How to Update UI while a task is running in another thread in android?

Arnab Kundu

I have tried updating my user interface using the text view what i have done is a run a while loop indefinitely and left the thread for sleep for one second..

 int i;
    while(true)
    {
        Thread.Sleep(1000);
        textView.setText(updateTime);
    }
Basheer Kohli

Try this one arnab...

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView textView = (TextView) findViewById(R.id.displayid);
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bundle = msg.getData();
            textView.setText(bundle.getString("mKey"));
        }
    };

}
public void press(View v) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                synchronized (this) {
                    try {
                        wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    bundle = new Bundle();
                    message = handler.obtainMessage();
                    simpleDateFormat = new SimpleDateFormat("hh:mm:ss YYYY/mm/dd", Locale.US);
                    date = simpleDateFormat.format(new Date());
                    bundle.putString("mKey", date);
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            }

        }
    }).start();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update progress bar in another form while task is running

From Dev

How to force a UI update during a lengthy task on the UI thread

From Dev

How to perform a task in another function and update the UI from there as the task progresses?

From Dev

how to stop main thread while another thread still running

From Dev

android: update UI from another thread in another class

From Dev

Pause Thread While Another Thread Is Executing A Task

From Dev

Pause Thread While Another Thread Is Executing A Task

From Dev

How to stop the task running in a Thread Pool Executor Android

From Dev

How to Update UI from Button Event (or another Task)

From Dev

How update textbox in a loop running on another thread in C#

From Dev

How to update progress bar while working in the UI thread

From Dev

C# - How to update Main UI from a thread in another class

From Dev

Matlab: How fork another thread to load data while the main thread keep on doing the major task?

From Dev

Error while exiting application with another thread running

From Dev

Running Firebase callback on another thread is blocking UI

From Dev

Update UI Thread while without freezing application

From Dev

Android - How to change UI from another thread in signed mode

From Dev

Refreshing UI while waiting for another thread to complete

From Dev

Update UI item from another class and thread

From Dev

How to update UI from thread

From Java

Android basics: running code in the UI thread

From Dev

Questions about services running on the UI thread in Android

From Dev

Showing a spinning wheel in the foreground while my task executes in the UI thread

From Dev

C# Threading/Async: Running a task in the background while UI is interactable

From Dev

C# Threading/Async: Running a task in the background while UI is interactable

From Dev

Is it a bug or a feature? In some cases it is possible to access the UI thread from a task not running on the UI thread

From Dev

If any worker thread can update UI in android?

From Dev

How to update the text a label from another Non UI thread in c#

From Dev

Releases UI thread while long method is running using await and async

Related Related

  1. 1

    Update progress bar in another form while task is running

  2. 2

    How to force a UI update during a lengthy task on the UI thread

  3. 3

    How to perform a task in another function and update the UI from there as the task progresses?

  4. 4

    how to stop main thread while another thread still running

  5. 5

    android: update UI from another thread in another class

  6. 6

    Pause Thread While Another Thread Is Executing A Task

  7. 7

    Pause Thread While Another Thread Is Executing A Task

  8. 8

    How to stop the task running in a Thread Pool Executor Android

  9. 9

    How to Update UI from Button Event (or another Task)

  10. 10

    How update textbox in a loop running on another thread in C#

  11. 11

    How to update progress bar while working in the UI thread

  12. 12

    C# - How to update Main UI from a thread in another class

  13. 13

    Matlab: How fork another thread to load data while the main thread keep on doing the major task?

  14. 14

    Error while exiting application with another thread running

  15. 15

    Running Firebase callback on another thread is blocking UI

  16. 16

    Update UI Thread while without freezing application

  17. 17

    Android - How to change UI from another thread in signed mode

  18. 18

    Refreshing UI while waiting for another thread to complete

  19. 19

    Update UI item from another class and thread

  20. 20

    How to update UI from thread

  21. 21

    Android basics: running code in the UI thread

  22. 22

    Questions about services running on the UI thread in Android

  23. 23

    Showing a spinning wheel in the foreground while my task executes in the UI thread

  24. 24

    C# Threading/Async: Running a task in the background while UI is interactable

  25. 25

    C# Threading/Async: Running a task in the background while UI is interactable

  26. 26

    Is it a bug or a feature? In some cases it is possible to access the UI thread from a task not running on the UI thread

  27. 27

    If any worker thread can update UI in android?

  28. 28

    How to update the text a label from another Non UI thread in c#

  29. 29

    Releases UI thread while long method is running using await and async

HotTag

Archive