package gr.mybook.mythreadexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Activity_1 extends Activity {
	Boolean flipText = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_1);
    }
    
    public void  onClick(View view) {
    	flipText = !flipText;
      	TextView textView = (TextView) findViewById(R.id.textView1);
        
    	if (flipText) {
    		delay();
        	textView.setText("REFRESH TEXT");
    	}
    	else {
    		delay();
   		    textView.setText(getString(R.string.text1));
    	}
    }
    	
    	public void delay() {
    		Runnable runnable = new Runnable() {
    			public void run() {
    	        	long upto= System.currentTimeMillis()+20000;
    	        	while (System.currentTimeMillis()<upto) {
    	             	try {
    	        		   wait(upto-System.currentTimeMillis());
    	             	}
    	             	catch (Exception e) {
    	        		// end thread
    	             	}
    	    	    }

    			}
    		};
    		Thread thread = new Thread(runnable);
    		thread.start();
    	}
}