Saturday 30 June 2012

Update UI using RunOnUiThread in Android

package com.umer.runOnUiThreadExmp;
 

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class RunOnUiThreadExample extends Activity {
    /** Called when the activity is first created. */
 TextView txtview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtview=(TextView)findViewById(R.id.txtview);
        myThread();
    }
    public void myThread(){
     Thread th=new Thread(){
       
      @Override
      public void run(){
       try
       {
        while(true)
        {
        Thread.sleep(100);
 
 
           RunOnUiThreadExample.this.runOnUiThread(new Runnable() {
             
         @Override
         public void run() {
          // TODO Auto-generated method stub
         // do ur UI work
          txtview.setText("my name is umer and i am android developer");
         }
        });
        }
       }catch (InterruptedException e) {
     // TODO: handle exception
    }
      }
     };
     th.start();
    }
}

No comments:

Post a Comment