In this post we will learn how to set images as wallpaper and develop a simple android application which sets images as wallpaper to our mobile devices. |
Firstly create a new android project,
click next 2 or 3 times until finish button appears.
Then first we desgin the layout i.e the xml part , here we have just one image view and one button to set that image as wallpaper.
We will set the image size to 200 * 200 (for the tutorial sake),
<ImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />
and one button ,
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set as wallpaper" android:id="@+id/button" android:layout_below="@+id/imageView" android:layout_centerHorizontal="true" />
Now lets do the java part.
Firstly initialize the imageview and button we used in activity_man.xml file
ImageView iv = (ImageView) findViewById(R.id.imageView); Button button = (Button) findViewById(R.id.button);
now copy any image you want and paste in drawable folder , i haved named my image as cc.jpg
Now set this image as the resource for the imageview we used
iv.setImageResource(R.drawable.cc);
Now we the button is clicked, this image will set as a wallpaper ,
here we convert the image into bitmap and then set that image as a wallpaper , for that we use BitmapFactory class.
First we take the input resource for the BitmapFactory , the input stream accepts only an int value as a paramater , so we store our image in an integer,
int wallpaper;at the beginning , just above the onCreate activity
and later when button is clicked set this as
wallpaper = R.drawable.cc;
now when the button is clicked,
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { InputStream is = getResources().openRawResource(wallpaper); Bitmap bm = BitmapFactory.decodeStream(is); WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); try { wallpaperManager.setBitmap(bm); Toast toast = Toast.makeText(MainActivity.this, "Set wallpaper successfully!", Toast.LENGTH_LONG); toast.show(); } catch (IOException e) { e.printStackTrace(); } } });
This will set the image as a wallpaper,
The complete MainActivity.java file is ,
package com.blogspot.codingcorner999.wallpaperapp; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; public class MainActivity extends ActionBarActivity { int wallpaper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewById(R.id.imageView); Button button = (Button) findViewById(R.id.button); iv.setImageResource(R.drawable.cc); wallpaper = R.drawable.cc; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { InputStream is = getResources().openRawResource(wallpaper); Bitmap bm = BitmapFactory.decodeStream(is); WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this); try { wallpaperManager.setBitmap(bm); Toast toast = Toast.makeText(MainActivity.this, "Set wallpaper successfully!", Toast.LENGTH_LONG); toast.show(); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
For setting wallpaper we need to add a permission to set wallpaper in manifest file as the android system must have all the information of the app before running the app. So all the activities , permissions must be included in manifest.xml file.
The permission we need is SET_WALLPAPER
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
The complete manifest file looks like,
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.codingcorner999.wallpaperapp" > <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Thats it we are done.
OUTPUT :