how to set image as wallpaper in android studio
Hello android developer. in this article, you will learn how to set an image as wallpaper in android studio using wallpaper manager in your android application. if you want to set your image in your mobile wallpaper then develop this application. this application will help you to set image on the android screen. wallpaper manager helps you to set your favorite in your mobile wallpaper.
please follow the code given below to set image as wallpaper in android studio
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.WallpaperManager; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.util.DisplayMetrics; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.IOException; public class SetPhoneWallpaper extends AppCompatActivity { Button button; ImageView imageView; WallpaperManager wallpaper ; Bitmap bitmap1, bitmap2 ; DisplayMetrics displayMetrics ; int width, height; BitmapDrawable bitmapDrawable ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set_phone_wallpaper); button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.imageView); wallpaper = WallpaperManager.getInstance(getApplicationContext()); bitmapDrawable = (BitmapDrawable) imageView.getDrawable(); bitmap1 = bitmapDrawable.getBitmap(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { GetScreenWidthHeight(); SetBitmapSize(); wallpaper = WallpaperManager.getInstance(SetPhoneWallpaper.this); try { wallpaper.setBitmap(bitmap2); wallpaper.suggestDesiredDimensions(width, height); } catch (IOException e) { e.printStackTrace(); } } }); } public void GetScreenWidthHeight(){ displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); width = displayMetrics.widthPixels; height = displayMetrics.heightPixels; } public void SetBitmapSize(){ bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:src="@drawable/project"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click here to Set Wallpaper" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="94dp" /> </RelativeLayout> |
Screenshot of this application