sign up
Stack Overflow
Questions Tags Users Badges Unanswered Ask
Read this post in our app!
Change layout background dynamically and automatically
android
android-layout
relativelayout
I have a Layout something like this.
xml
<RelativeLayout xmlns:android="[Link]
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
log in
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
private void TimerMethod()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO after 9 sec
[Link](images[getRandomNumber()]);
}
}, 9000);
Here is the Log Trace
01-04 [Link].307: E/AndroidRuntime(30200): FATAL EXCEPTION: Timer-0
01-04 [Link].307: E/AndroidRuntime(30200): [Link]: Can't create handler inside thread that has not called [Link]()
01-04 [Link].307: E/AndroidRuntime(30200): at [Link].<init>([Link])
01-04 [Link].307: E/AndroidRuntime(30200): at [Link]([Link])
01-04 [Link].307: E/AndroidRuntime(30200): at [Link]$0([Link])
01-04 [Link].307: E/AndroidRuntime(30200): at [Link]$[Link]([Link])
01-04 [Link].307: E/AndroidRuntime(30200): at [Link]$[Link]([Link])
what i want to try is to change it automatically while on the activity.
share
improve this question
AndroidNewbie
206 2 8 17
Asked
Dec 27 '13 at 4:58
Edited
Jan 3 '14 at 17:10
hey you want to perform this continuously after some interval or only when user comes back to the activity ? AndyN Dec 27 '13 at 5:33
example situation.. Im in my app homepage, and while im on it my background is continuously changing after some interval. AndroidNewbie Dec 27 '13 at 5:36
then for such situation you should make use of timer to achieve this. AndyN Dec 27 '13 at 5:39
@N2P are talking same with zanky answer? AndroidNewbie Dec 27 '13 at 5:40
yes refer to zanky's answer. It'll solve your problem. add that snippet to your onCreate() method. AndyN Dec 27 '13 at 5:44
show 1 more comment
4 Answers
Order By
Votes
onCreate() is called only once, when the Activity is first opened. After you navigate away, onPause() is called and later when you return to the
Activity, onResume() is called.
So, to change the background every time you navigate to the Activity, shift your code to change the background from onCreate() to onResume().
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
relativeLayout = (RelativeLayout) findViewById([Link]);
images[] = {[Link].image1, [Link].image2, [Link].image3, [Link].image4};
protected void onResume()
{
if(relativeLayout != null)
[Link](images[getRandomNumber()]);
}
private int getRandomNumber() {
//Note that general syntax is Random().nextInt(n)
//It results in range 0-4
//So it should be equal to number of images in images[] array
return new Random().nextInt(4);
}}
share
improve this answer
Swayam
11.6k 6 43 77
Answered
Dec 27 '13 at 5:00
Edited
Dec 27 '13 at 10:33
may i ask for a sample snips? thanks AndroidNewbie Dec 27 '13 at 5:24
Updated my answer. That should do it. Swayam Dec 27 '13 at 5:27
there is an error on images [Link](images[getRandomNumber()]); AndroidNewbie Dec 27 '13 at 5:33
Yeah, okay. In my haste, I forgot to declare images[] globally. I thought you might figure that out. Anyway, fixed the declaration. Check the answer now. Swayam Dec 27 '13 at
10:26
add a comment
You can do this by making use of Timers and Handlers Try this code:
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
private void TimerMethod()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//TODO after 9 sec
[Link](images[getRandomNumber()]);
}
}, 9000);
share
improve this answer
zanky
1,745 4 31 76
Answered
Dec 27 '13 at 5:13
Edited
Dec 30 '13 at 5:10
thanks to this but where should i call its function? AndroidNewbie Dec 27 '13 at 5:22
Put this code in onCreate() method of activity. zanky Dec 27 '13 at 5:27
pardon :) TimerMethod() must be called in onCreate()? snips please? thank you. AndroidNewbie Dec 27 '13 at 5:35
i dont get it how to combine this with my code. sorry. AndroidNewbie Dec 27 '13 at 6:13
there is something wrong with the code sir it says cannot call handler because your not using looper "something".. AndroidNewbie Dec 29 '13 at 5:43
show 5 more comments
share
In onCreate Method your code will execute only once and if you want it to change after some time interval then use TimerTask or you can use
Handler also.
improve this answer
Jitesh Dalsaniya
2,247 9 30
may i ask for a sample snips? thanks AndroidNewbie Dec 27 '13 at 5:23
See @zanky code for sample snips :) Jitesh Dalsaniya Dec 27 '13 at 5:27
Answered
Dec 27 '13 at 5:12
add a comment
Try calling your [Link](images[getRandomNumber()]); on your activity's onResume() method like this.
protected void onResume()
{
if(relativeLayout != null){
[Link](images[getRandomNumber()]);
}
}
Hope this helps.
share
improve this answer
AndyN
1,218 1 6 23
Answered
Dec 27 '13 at 5:07
Edited
Dec 27 '13 at 5:31
may i ask for a sample snips? thanks AndroidNewbie Dec 27 '13 at 5:25
see , i've edited my answer. AndyN Dec 27 '13 at 5:31
add a comment
Your Answer
log in
or
Name
Email
By posting your answer, you agree to the privacy policy and terms of service.
Post Your Answer
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc