[TUT] Preloaded images set as phone wallpaper

This is the first request that I’ve done!

This tutorial will show you how to display some images within your application from the resources folder, you can browse through these images and choose to set one as your phones wallpaper.

Tutorial will go like this:

  • Create XML layout to let you browse your images
  • Create main activity to load these images
  • Create helper to do the hard work of setting the wallpaper
  • Add permissions to manifest

So here .. we .. go

The aim of this is to be able to browse through some pictures you have in your resources folder. Therefore your XML layout needs an ImageView to show the picture and some buttons to go backwards forwards and set it as the wallpaper. I’ve used weighting on the buttons don’t worry if you haven’t seen it before it’s easy. The three buttons are in a linearlayout that is the width of the screen, so thats 100% width. Each button’s width is set to 0dip this means ithe buttons width is zero! However when you then add the layout_weight attribute it’s width becomes a percentage. So the previous and next buttons take up 25% of the width and the set wallpaper button the other 50%! Told you it was easy.

activity_main.xml

Next comes your Main activity. This will be in charge of letting you browse through the images, displaying them on the screen. For this tutorial the images haven’t been cached in the activity, only the resource Id has, this means each time you browse to a new image the system has to go fetch it out of your resource folder. The benefits of this are you don’t have a massive memory footprint in your activity (a big list holding images), the downside is .. you don’t have a big list holding images. So it’s give and take, the more images you want to browse through, the more the current way it is written makes sense. The other special item in this activity is the callback mechanism from the heavy lifter. Our heavy lifter is there so we don’t do too many complex operations on the main thread, they are delegated to another thread so the user can carry on browsing. When these threads have finished their task they need to inform the activity, this is where the handler comes in. It’s all commented so your bound to get it:

MainActivity.java

Once we have an activity we want to create the lifter so we can actually set the wallpaper. The heavy lifter ( I couldn’t think of a better name), gets a hold of the systems wallpaper manager and uses this to set the wallpaper. We do it in a separate thread for the reasons discussed above.

HeavyLifter.java

Yayy all done, wait … it doesn’t work! Ah those bleeding manifest permissions! Never forget 🙂

AndroidManifest.xml

Here’s the source project code: Downlod Set Wallpaper Tutorial Source Code

Hope you enjoyed it, please say thanks if your using it.

14 thoughts on “[TUT] Preloaded images set as phone wallpaper

  1. this.manager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);

Comments are closed.