firstly add this permission in AndroidManifest.xml
file
<uses-permission android:name="android.permission.SET_WALLPAPER">
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_margin="20dp"
android:id="@+id/set"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set WallPaper" />
<ImageView
android:layout_margin="20dp"
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
then below is your java code which you to implement:
public class SetWallpaper extends Activity {
Bitmap bitmap;
int lastImageRef;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.ic_launcher);
imagePreview.setImageResource(R.drawable.five);
buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(R.drawable.ic_launcher);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
}