Set An Image As Wallpaper In Android
Last Updated: September 24, 2012
In this tutorial, you will learn how to set an image as wallpaper using WallpaperManager in your Android application. WallpaperManager allows you get the current desired dimension wallpaper and set it as wallpaper on your Android device. We will create an actionbar menu and on menu item click will specify an image path to WallpaperManager and set the image as wallpaper. So lets begin..
Create a new project in Eclipse, select File > New > Android Application Project. Fill in the details and name your project WallpaperTutorial.
Application Name : WallpaperTutorial
Project Name : WallpaperTutorial
Package Name : com.androidbegin.wallpapertutorial
Open your MainActivity.java and paste the following code.
MainActivity.java
package com.androidbegin.wallpapertutorial; import java.io.IOException; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.WallpaperManager; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the view from activity_main.xml setContentView(R.layout.activity_main); // Create an actionbar ActionBar actionBar = getActionBar(); actionBar.show(); // Locate ImageView in activity_main.xml ImageView mywallpaper = (ImageView) findViewById(R.id.wallpaper); // Attach image into ImageView mywallpaper.setImageResource(R.drawable.wallpaper); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Create an actionbar menu menu.add("Set as Wallpaper") // Add a new Menu Button .setOnMenuItemClickListener(this.SetWallpaperClickListener) .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); return super.onCreateOptionsMenu(menu); } // Capture actionbar menu item click OnMenuItemClickListener SetWallpaperClickListener = new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { // Retrieve a WallpaperManager WallpaperManager myWallpaperManager = WallpaperManager .getInstance(MainActivity.this); try { // Change the current system wallpaper myWallpaperManager.setResource(R.drawable.wallpaper); // Show a toast message on successful change Toast.makeText(MainActivity.this, "Wallpaper successfully changed", Toast.LENGTH_SHORT) .show(); } catch (IOException e) { // TODO Auto-generated catch block } return false; } }; }
We have created an ImageView to show the attached image on the graphical layout and an actionbar menu. On actionbar menu item click will use WallpaperManager to set the specified image as wallpaper.
We have prepared a sample wallpaper for this tutorial. Insert your downloaded sample wallpaper into your res > drawable-hdpi.
Sample Wallpaper
[wpfilebase tag=file id=78 tpl=download-button /]
Next, create an XML file for your MainActivity Graphical Layout. Go to res > layout > Right Click on layout > New >Android XML File
Name your new XML file activity_main.xml and paste the following code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/wallpaper" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Next, change the application name. Open your strings.xml in your res > values folder and paste the following code.
strings.xml
<resources> <string name="app_name">Wallpaper Tutorial</string> <string name="menu_settings">Settings</string> </resources>
In your AndroidManifest.xml, we need to declare a permission to allow the application to set an image as wallpaper. Open your AndroidManifest.xml and paste the following code.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidbegin.wallpapertutorial" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.SET_WALLPAPER" > </uses-permission> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:uiOptions="splitActionBarWhenNarrow" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Output:
Source Code
[purchase_link id=”7825″ text=”Purchase to Download Source Code” style=”button” color=”green”]
Hello.How can i add more wallpapers?
deni777
Set An Image As Wallpaper In Android
Hi spitfire10, I suggest you start learning from our previous post regarding gridview here https://www.androidbegin.com/tutorial/android-simple-gridview-tutorial/ . Then show me what you have tried to implement set image as wallpaper. If you encounter any issues, feel free to ask here. I believe doing this will help you learn better. :)
AndroidBegin
Set An Image As Wallpaper In Android
Hi spitfire10, I'm not sure which part you do not understand. However, I believe you are trying to create a wallpaper app, may I know where are you going to keep those images?
AndroidBegin
Set An Image As Wallpaper In Android
Hi AndroidBegin,I did try to study it for few days but seem that I not really understand. Can you please show me an example?Sorry for troubling you.Appreciated if you could point me a way.ThanksFire
spitfire10
Set An Image As Wallpaper In Android