I am making an application in which i am changing the the layout background continually like flipping the background, I am implementing the background change using Activity.runOnUiThread() function since it is a UI function and waiting for 2 seconds using Thread().sleep() but the application only shows the layout color I mentioned in the end.
package com.tutorial.flasher;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
//import android.os.SystemClock;
//import android.os.SystemClock;
import android.widget.LinearLayout;
public class flasher extends Activity {
/** Called when the activity is first created. */
LinearLayout llaLayout;
Thread th = new Thread("ThreadOne");
Activity _activity = new Activity();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
llaLayout = (LinearLayout)findViewById(R.id.layo);
Thread t1 = new Thread(new Runnable() {
public void run() {
try
{
Thread.sleep(2000);
}
catch (Exception e) {
// TODO: handle exception
}
}
});
t1.start();
t1.run();
t1.stop();
_activity.runOnUiThread(new Runnable(){
public void run(){
llaLayout.setBackgroundColor(Color.parseColor("#00FF00"));
}
});
//t1.currentThread();
t1.start();
t1.run();
t1.stop();
_activity.runOnUiThread(new Runnable(){
public void run(){
//Color BLUE of layout
llaLayout.setBackgroundColor(Color.parseColor("#0000FF"));
}
});
}
}
Both the UI changes and Thread staring would be happening in a loop(which is not shown) but still the application is changing the layout color only once.
Thanks,
Sid
newActivity, Activity is created by Android system.Thread.startwill start the thread, there is no need to callThread.run.Activity.runOnUiThreadmaybe not the best way to archive this, tryHandler. Here is sample code: