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:
- Android Wallpaper App Example 1: Android GridView Example in Real App
- Android Wallpaper App Example 2: Download Image with Progress Dialog
- Android Wallpaper App Example 3: Save And Load Downloading File Locally
- Android Wallpaper App Example 4: Load Images And Set Wallpaper
- Android Wallpaper App Example 5: Crop Image in Android
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(); } }
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
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;
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 😉
Hi,
I think get this line:
adView = new AdView(this, AdSize.SMART_BANNER, "a152f84b0f4f9ed");
Change the “a152f84b0f4f9ed” with your own one.
Thanks
Ok, I did that with mine but the mine lokks like (ca-app-pub-XXXXXXXX/XXXX) is it good? 🙂
Yes. It is the new version of Admob ID
how can i bought this app?
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.
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 ! 😉
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
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
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
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.
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
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. 🙂
just bought! waiting for next update ‘cropping feature’
The crop image feature integrated here:
http://jmsliu.com/2455/android-crop-image-and-assign-wallpaper.html
Can you help me fixing this error please
Error:(78, 40) error: cannot find symbol variable imageBitmap
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
Hi, ibought your app. How can i make the gallery from in app images? I don’t need a downloadable images. Thanks
Oh, you wanna make a gallery. Please check this post: Android Gallery Example: Widget Gallery
Hi again.
I fix my problems thanks. How can i change the grid view size?
I need more big picture preview.
change the ImageView size in the layout file