Free Education

Online Education

Android Studio

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)

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)

<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

 

Leave a Reply

Your email address will not be published. Required fields are marked *