Home > Android > Wallpaper Example in Android Programming

Wallpaper Example in Android Programming

Posted by  on June 25, 2012, filed in: Android DevelopmentAndroid Programming Tutorials

This example explains how to set wallpaper programmatically for home screen in android.


1.) Create a new project by File-> New -> Android Project name it WallpaperExample.

2.) Write following into main.xml:

<?xml version="1.0" encoding="utf-8"?>
        android:id="@+id/imageview" />
            android:layout_gravity="bottom" />
            android:text="Set as Wallpaper"
            android:layout_gravity="bottom" />

3.) Set following permission to you manifest file:

<uses-permission android:name="android.permission.SET_WALLPAPER" />

4.) Run for output.


1.) Create a project named WallpaperExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: WallpaperExample
Package Name: com. example. WallpaperExample
Activity Name: WallpaperExample
Min SDK Version: 14

2.) Open WallpaperExample.java file and write following code there:

package com.example.WallpaperExample;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class WallpaperExample extends Activity {
    final static private int[] mColors =
    {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
        Color.YELLOW, Color.WHITE};
    protected void onCreate(Bundle savedInstanceState) {
        // Be sure to call the super class.
        // See res/layout/wallpaper_2.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
        final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);
        Button randomize = (Button) findViewById(R.id.randomize);
        randomize.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                int mColor = (int) Math.floor(Math.random() * mColors.length);
                wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
        Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
        setWallpaper.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                try {
                } catch (IOException e) {

3.) Compile and build the project.



Source: http://www.edumobile.org/android/android-development/wallpaper-example-in-android-programming/

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: