I have a Layout something like this.
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mylayout" > </RelativeLayout>
java - Then you can dynamically change the background of the layout using below code
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
private Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
}
private void TimerMethod()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO after 9 sec
relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}
}, 9000);
}
}
Here is the Log Trace
01-04 01:08:15.307: E/AndroidRuntime(30200): FATAL EXCEPTION: Timer-0
01-04 01:08:15.307: E/AndroidRuntime(30200): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-04 01:08:15.307: E/AndroidRuntime(30200): at android.os.Handler.<init>(Handler.java:121)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity.TimerMethod(LoginActivity.java:55)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity.access$0(LoginActivity.java:53)
01-04 01:08:15.307: E/AndroidRuntime(30200): at info.androidhive.slidingmenu.LoginActivity$1.run(LoginActivity.java:48)
01-04 01:08:15.307: E/AndroidRuntime(30200): at java.util.Timer$TimerImpl.run(Timer.java:284)
what i want to try is to change it automatically while on the activity.