5

I want to change background image of my application when user select the image from setting activity, I set the background image using theme as:

<style name="Theme" parent="@android:style/Theme">
    <item name="android:windowBackground">@drawable/sunset</item>
</style>

Now how can I change the android:windowBackground via code.

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62

2 Answers2

1

From your activity, call getWindow().setBackgroundDrawable(Drawable drawable) or getWindow().setBackgroundDrawableResource(int resid).

Gallal
  • 4,267
  • 6
  • 38
  • 61
0

Updated

after some R&D and example, i got is to solve this issue you have to do as follow:

First create the Theme. (You have already created your Theme)

Then get The Theme at runTime and set the Background of that Theme as you want.

Then set the Theme as Application Theme.

Above steps is what you can do to get Change the Background of whole of your app.

For to getTheme and set the attribute value see this: This Link

Also See this.

Community
  • 1
  • 1
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • So what is the best way to follow to achieve my target with less efforts. – Krishnakant Dalal Apr 24 '12 at 09:19
  • Thanks for your reply but I don't want to restrict user to select few Background Images, he can explore SD Card and select any image as its background – Krishnakant Dalal Apr 25 '12 at 05:47
  • I haven't seen any way to set the background of a theme at run-time yet in any of your links. Same thing when I googled it. Could you be more precise and add some code example? – ForceMagic Nov 04 '13 at 14:37
  • @ForceMagic: I don't know any method to set Application Background at runtime. but you can set the Background of individual activity. For that you can use: getWindow().setBackgroundDrawable(Drawable.createFromPath(bg_path)); – Shreyash Mahajan Nov 06 '13 at 09:29
  • Then I'm not sure to understand how you would `Then get The Theme at runTime and set the Background of that Theme as you want.` since I haven't seen any way to set the background of a Theme. – ForceMagic Nov 06 '13 at 15:43