user1747819
user1747819

Reputation: 311

How to Update a Textview continuosly

I want to update TextView or Button from a loop as some changes occur. But this throws an exception.

public void random() 
{      
    Thread timer = new Thread(){
        public void run()
        {
            Random ran= new Random();                        
            {
                final int ranNum=ran.nextInt(8);        
                while (true)
                {
                    Update = (TextView) findViewById(R.id.UpdateStatus);
                    Update.setText(ranNum); // TODO Auto-generated method stub
                    Thread.Sleep(1000);
                }                   
            }
        }
    };                               
    timer.start();   
}

Why does the code throw an exception?

Upvotes: 0

Views: 82

Answers (5)

user3865434
user3865434

Reputation: 65

You cannot use while(true) in android! Android handler don't let you to fall a action into loop that can't come out from it. Use Service for that!

Upvotes: 0

Subtain Ishfaq
Subtain Ishfaq

Reputation: 841

Android is thread sensitive , you can not update UI thread from any other thread android wont allow you to do that, that is why your app is crashing . try to create final Handler handler = new Handler(); Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.then try this

 `Thread timer = new Thread(){
   public void run()
   {
    Random ran= new Random();                     
  {
final int ranNum=ran.nextInt(8);        
  while (true)
  { 
handler.post(new Runnable() {
public void run() {
TUpdate = (TextView) findViewById(R.id.UpdateStatus);
Update.setText(ranNum); // TODO Auto-generated method stub
 Thread.Sleep(1000);

 }
 }
     }
   }
       };`

Upvotes: 0

FreewheelNat
FreewheelNat

Reputation: 2937

To edit the TextView, you need to run the code in the UI thread, so you need to think of a setup where you don't need to sleep the thread (ie not using Thread.Sleep(1000);).

You should use a Handler and a Runnable, with the code inside the runnable posting to itself every x milliseconds, using handler.postDelayed method (check http://developer.android.com/reference/android/os/Handler.html).

Note that with this set up, the code will keep on running even after the TextView isn't accessible (for example, the user has moved to another activity) so make sure you do a check for this and create a stop condition (perhaps, by using a boolean variable isVisible that you set true/false in the onResume/onPause of your activity or fragment).

Upvotes: 0

Arash GM
Arash GM

Reputation: 10395

You cannot update UI components from background threads. write a Handler for the purpose of updating TextView.

Handler RefreshHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            Update = (TextView) findViewById(R.id.UpdateStatus);
            Update.setText(msg);
        }
    };

and in your runnable you can just send your message to handler to update:

public void random() 
{      
Thread timer = new Thread(){
   public void run()
   {
       Random ran= new Random();                     
                {
            final int ranNum=ran.nextInt(8);        
           while (true)
           {
            RefreshHandler.sendMessage(msg);

            Thread.Sleep(1000);
               }                
    }
   }
};                                
timer.start();   
}

Upvotes: 1

Illegal Argument
Illegal Argument

Reputation: 10338

Try handler for updating textview:

Handler handler = new Handler();    
Runnable runnable = new Runnable() {
@Override
public void run() {
  //update your ui
  handler.postDelayed(this, 100);//this restarts the handler better to have some terminating condition
}
};
handler.postDelayed(runnable, 100);//starts the handler first time

Upvotes: 0

Related Questions