DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Setting Custom Wallpaper with Android

Setting Custom Wallpaper with Android

Mark Mooibroek user avatar by
Mark Mooibroek
·
Jan. 27, 12 · Interview
Like (0)
Save
Tweet
Share
12.82K Views

Join the DZone community and get the full member experience.

Join For Free

Wallpapers are a nice way to customize your homescreen. There are many applications that allow you to change your wallpaper and they have huge databases with alot of wallpapers. An example is Zedge.

But the main question is:
How to they do it?

If we take a look at how this is done, it's pretty simple. In this tutorial i'll explain the basics ( really the basics :-) ) of how you can make your own Wallpaper-changer-application-thingy!

This tutorial will cover the following subjects:

  • A GridView to  show the wallpapers
  • Implement a custom Adapter to show our images
  • The code required to set a new wallpaper


Follow me after the break to learn how I put this together.

The Layout

The layout is pretty simple here. Just a basic GridView. We will load the images in the Grid in the next chapter ( The custom adapter ).

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>


The custom ImageAdapter

Here is where the GridView logic is. This will load the images. In this example I chose to make all images embedded and static. The main thing is to notice the Arrays in the bottom. They hold the reference id's to the thumbnails and the large images.

public class ImageAdapter extends BaseAdapter {
	private Context mContext;

	public ImageAdapter(Context c) {
		mContext = c;
	}

	public int getCount() {
		return mThumbIds.length;
	}

	public Object getItem(int position) {
		return null;
	}

	//Don't return the thumb id - but the large image id
	public long getItemId(int position) {
		return mFullSizeIds[position];
	}

	// create a new ImageView for each item referenced by the Adapter
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;

		if (convertView == null) {
			imageView = new ImageView(mContext);
			imageView.setLayoutParams(new GridView.LayoutParams(300, 250));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}

		imageView.setImageResource(mThumbIds[position]);

		return imageView;
	}

	// references to our smaller images ( else the memory will fill up )
	private Integer[] mThumbIds = {
			R.drawable.wallpaper1t,
			R.drawable.wallpaper2t,
			R.drawable.wallpaper3t,
			R.drawable.wallpaper4t,
			R.drawable.wallpaper5t,
			R.drawable.wallpaper6t,
			R.drawable.wallpaper7t,
			R.drawable.wallpaper8t
	};

	//These id we will pass on getItemid ( the larger images )
	private Integer[] mFullSizeIds = {
			R.drawable.wallpaper1,
			R.drawable.wallpaper2,
			R.drawable.wallpaper3,
			R.drawable.wallpaper4,
			R.drawable.wallpaper5,
			R.drawable.wallpaper6,
			R.drawable.wallpaper7,
			R.drawable.wallpaper8
	};
}


The magic code

The actual setting of the wallpaper is done in only a few lines of code. This is the code that binds the Adapter to the Gridview + it implements a setOnItemClickListener. This will tell the application what to do when a user clicks on a image.

//find grid and apply the custom adapter
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

//set the onclickhandler ( what happens when i click a item in the grid )
gridview.setOnItemClickListener(new OnItemClickListener() {
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

		//Make a Bitmap from the Resource
		ImageAdapter i = (ImageAdapter)parent.getAdapter();
		Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));

		//Get the WallpaperManager
		WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

		try {
			//Set the clicked bitmap
			myWallpaperManager.setBitmap(mBitmap);
			Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
		}
	}
});


Sidenote: We also have to include a permission. This permission is located in the AndroidManifest.xml

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


Make it more interesting

Here are some ideas on how you can improve this application:

  • Dynamically loading images from the internet
  • Call Flickr for your images
  • Let a ProgressBar popup when you click a image
  • Make thumbnails dynamically
  • etc etc


If you alter the project and want to share it. Please post the relevant code on PasteBin and leave a comment.


Source code

You can download the source code here:

  • Eclipse project WallpaperApp

 

Source: http://p-xr.com/android-tutorial-how-to-set-a-custom-wallpaper-gridview/

code style Android (robot) application Database Eclipse IT POST (HTTP) Download Internet (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is DevOps Dead?
  • Testing Repository Adapters With Hexagonal Architecture
  • Create Spider Chart With ReactJS
  • Multi-Cloud Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: