Wallpaper Example in Android Programming

1

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

Algorithm:

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"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/randomize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Randomize"
            android:layout_gravity="bottom" />
        <Button
            android:id="@+id/setwallpaper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Set as Wallpaper"
            android:layout_gravity="bottom" />
    </LinearLayout>
</FrameLayout>

3.) Set following permission to you manifest file:

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

4.) Run for output.

Steps:

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};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Be sure to call the super class.
		super.onCreate(savedInstanceState);
		// See res/layout/wallpaper_2.xml for this
		// view layout definition, which is being set here as
		// the content of our screen.
		setContentView(R.layout.main);
		final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
		final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
		final ImageView imageView = (ImageView) findViewById(R.id.imageview);
		imageView.setDrawingCacheEnabled(true);
		imageView.setImageDrawable(wallpaperDrawable);

		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);
				imageView.setImageDrawable(wallpaperDrawable);
				imageView.invalidate();
			}
		});

		Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
		setWallpaper.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				try {
					wallpaperManager.setBitmap(imageView.getDrawingCache());
					finish();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}
}

3.) Compile and build the project.

Output

  • Alexei

    How to insert HTML5 into live wallpaper?

Proudly designed by Theme Junkie.