0

I searched a lot but I couldn't know how to do this, if anyone has a way to do that

here is my code :

public class MainActivity extends AppCompatActivity {

Button btn ;
    String url;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
btn=findViewById(R.id.btnsetwallpaper);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        //some code here so that I can set the wall paper of the phone using url

    }
});

    }
}
  • I think this might help you https://stackoverflow.com/questions/33971626/set-background-image-to-relative-layout-using-glide-in-android Good luck☺️ – בר ונטורה Mar 26 '20 at 13:14

1 Answers1

0

Add permission in manifest

 <uses-permission android:name="android.permission.SET_WALLPAPER"/>

Implement Picasso to help you, docs : https://square.github.io/picasso/

Add this AsyncTask class in your MainActivity

public class SetWallpaper extends AsyncTask<String, Void, Bitmap> {

        ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = null;
            try {
                bitmap = Picasso.get().load(params[0]).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        protected void onPostExecute (Bitmap result) {
            super.onPostExecute(result);

            WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
            try {
                wallpaperManager.setBitmap(result);
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Wallpaper changed", Toast.LENGTH_SHORT).show();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        protected void onPreExecute () {
            super.onPreExecute();

            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading image...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }
    }

Then call it in your button

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetWallpaper sw = new SetWallpaper();
                sw.execute(url);
            }
        });

Hope it helps you!

Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17