-
-
Save codingwithsara/daf8ce5194d7af6af68b8e5a04d2382b to your computer and use it in GitHub Desktop.
How to Change Wallpaper in Android Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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