Change Layout Background Dynamically and Automatically - Stack Overflow PDF
Change Layout Background Dynamically and Automatically - Stack Overflow PDF
Stack Overflow
Questions Tags Users Badges Unanswered Ask
android-layout
relativelayout
java - Then you can dynamically change the background of the layout using below code
log in
@Override
public void run()
{
TimerMethod();
}
}, 0, 9000);
}
}, 9000);
share
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
share
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
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);
}
}, 9000);
share
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.
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 relativeLayout.setBackgroundResource(images[getRandomNumber()]); on your activity's onResume() method like this.
share
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
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc