New Update Version 1.3:

  • Move Set Wallpaper Menu on Action Bar for Android 3.0 and later
  • Add Toast message when wallpaper is set successfully

In the last android example code, I have shown you how to download images with Progress Dialog and save images in local on Android. After downloading the image, I preview the image in a new activity. In this example, I will show you how to load the image from local and set it as wallpaper in Android. It is also the last job of my Android Wallpaper App. To complete this job, I will attach a menu on the activity. After you click on set wallpaper menu item, the new wallpaper will be set as current image.

This Android Wallpaper App example includes four tutorials:

Add Set Wallpaper Menu

In my android Wallpaper app, I will add a menu to set the wallpaper. From Android API 11 (Android 3.0), android supports ActionBar, which can display the activity title, navigation modes, and menu items on the top. In my app, I will show wallpaper menu on action bar if it is available. Otherwise, it will be in the menu in early android system. Now, let’s see this menu item example code:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_back"
	      android:icon="@drawable/ic_action_picture"
	      android:title="@string/back_to_Main"
	      android:showAsAction="never"/>
	<item android:id="@+id/action_setwallpaper"
	      android:icon="@drawable/ic_action_picture"
	      android:title="@string/action_setwallpaper"
	      android:showAsAction="ifRoom|withText"/>
</menu>

Attach Menu in Android Activity

After we define the menu in xml, we have to attach the menu by overriding the function onCreateOptionsMenu(). The following example code will show your how to attach a menu in Android:

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

Handle Menu Click Event in Android

The following code will show you how to handle the menu item click event in an activity. To identify which menu item is clicked, we are using the menu id to check each coming event.

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_setwallpaper:
			setWallPaper();
			return true;
		case R.id.action_back:
			finish();
			return true;
		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

Set Bitmap As Wallpaper in Android

When user clicks set wallpaper on the actionbar or in the menu, the above function onMenuItemSelected() will be called. The following code will show you how to set Wallpaper by given bitmap data.

private void setWallPaper() {
	WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
	try {
		wallpaperManager.setBitmap(imageBitmap);
		Toast toast = Toast.makeText(this, "Set wallpaper successfully!", Toast.LENGTH_LONG);
		toast.show();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

1

Try Example Android App

You can download and install this android app by click the following link, or scan the following QR code to download and install on your android phone simply. If you feel it is helpful, please support me by clicking the banner inside the app.
Download and Run This Example on Your Android Phone
chart

Get Full Source Code under $5.99


You will get whole Android Wallpaper App source code at $5.99. With this source code, you can do everything you want:

  • Load your own wallpaper;
  • Monetize the app with your own AdMob publish id;
  • Use the source code in your own project;
  • Publish this app in your android develop account;
Previous PostNext Post

22 Comments

  1. Hi,
    I bought you the complete code, good job ;). So I understood all, but the only thing i didn’t is about the admob :p, where Do I put my ID ? Did I need to add lines of code ?

    Answer please 😉

    1. Hi,

      I think get this line:

      adView = new AdView(this, AdSize.SMART_BANNER, "a152f84b0f4f9ed");

      Change the “a152f84b0f4f9ed” with your own one.

      Thanks

      1. Ok, I did that with mine but the mine lokks like (ca-app-pub-XXXXXXXX/XXXX) is it good? 🙂

          1. Please click the orange “Pay with PayPal” button in the end of the article. Once you successfully make the purchase, you can download the app in the same page.

  2. I have 2 questions:
    How to resize the miniature ? (they are to small I think)
    How to trim the picture before set it to wallpaper ? (cause that zoom on pictures and the quality become bad)

    Thank for answering me ! 😉

    1. Hello,

      You can set the thumbnail image size in the layout xml. For cropping image features, I will develop one in the future. Currently, there is no such function yet.

      Best Regards
      James

      1. Big thanks to you James ;), I did it for thumbnail. Yes with the cropping it will be a perfect app ;). I say thanks you for the work you ever did and thank you for the work you will do 🙂

        Best Regards
        Thibaut

  3. Hello james,

    I have a problem, when I apply an picture, the become wallpaper but the problem is, it make a zoom on the picture. So can we apply the wallpaper without a “zoom” cause the quality of the picture become poor :/

    Thanks

    1. Hello,

      You are right. The Android system will automatically stretch the image if you wallpaper is not fit the screen. Therefore you have to make sure the image is well designed to fit the screen.

      To fit the android screen, you need to design your wallpaper resolution 2 times wider than the android screen resolution.

      For example, if your android screen size is 480×800, your wallpaper shall be 960×800, and so forth.

      1. Thanks dude 😉 it works ;).
        This is a solution 😉

        You said me previously you will develop a function to crop picture by ourselves, it is still on developping ? 🙂

        Thanks

        1. I am too busy to do the cropping function currently. If I finish the cropping feature, I will update in this page. I am for sure to let you know. 🙂

    1. imageBitmap is a class variable. I suggest you to get my full source code. It includes all the source code I mentioned in my tutorials.

      Or you can just follow the whole serial tutorials to finish the project.

      Best Regards

Leave a Reply to James Cancel reply

Your email address will not be published. Required fields are marked *