Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Change Wallpaper in Android Studio
package com.codingwithsara.changewallpaper;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private WallpaperManager wallpaperManager = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wallpaperManager = WallpaperManager.getInstance(this);
}
public void changeWallpaper(View view) {
try {
switch (view.getId()) {
case R.id.blue:
Bitmap blueImage = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
wallpaperManager.setBitmap(blueImage);
Toast.makeText(this, "Blue", Toast.LENGTH_SHORT).show();
break;
case R.id.green:
Bitmap greenImage = BitmapFactory.decodeResource(getResources(), R.drawable.green);
Bitmap greenScaled = Bitmap.createScaledBitmap(greenImage,
greenImage.getWidth()/2, greenImage.getHeight()/2, true);
wallpaperManager.setBitmap(greenScaled);
Toast.makeText(this, "Green", Toast.LENGTH_SHORT).show();
break;
case R.id.orange:
Bitmap orangeImage = BitmapFactory.decodeResource(getResources(), R.drawable.orange);
Bitmap orangeScaled = Bitmap.createScaledBitmap(orangeImage,
orangeImage.getWidth()/3, orangeImage.getHeight()/3, true);
wallpaperManager.setBitmap(orangeScaled);
Toast.makeText(this, "Orange", Toast.LENGTH_SHORT).show();
break;
case R.id.clear:
wallpaperManager.clear();
Toast.makeText(this, "Clear", Toast.LENGTH_SHORT).show();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.