How to set a wallpaper through image link in android programmatically?
Asked Answered
S

3

3

I am trying to set as wallpaper through image link.

My Question: How to show dialog box "loading Image from server" and now "Set wallpaper". After this Pop up appear to ask user where they want to set

enter image description here

Current Situation: when user click the button "Set wallpaper". It only shows the dialog message and keep loading. then user need to dismiss the pop up and click again. Which means they need to click 2 times to set wallpaper. I want to make them easy to become more transparent.

Basically i want my user know which process is running in android

Here is below code

progressDialog = ProgressDialog.show(getActivity(),"Please wait...","Set as your wallpaper...",false,true);
               

                Picasso.get()
                        .load(setImgLink)
                        .into(new Target() {
                            @Override
                            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                                WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity());

                                try {
                                    if (bitmap != null) {
                                        wallpaperManager.setBitmap(bitmap);
                                        Snackbar.make(relative_image_slider, "Wallpaper was set", Snackbar.LENGTH_SHORT).show();
                                        progressDialog.dismiss();
                                        ct_popup.setText("Wallpaper was set");
                                        final AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
                                        builder1.setView(subView);
                                        builder1.setPositiveButton("Close", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.cancel();
                                                ((ViewGroup)subView.getParent()).removeView(subView);
                                            }
                                        });
                                        builder1.setIcon(R.mipmap.ic_launcher);
                                        builder1.setCancelable(false);
                                        builder1.create();
                                        builder1.show();
                                    }else {
                                        Toast.makeText(getActivity(), "Unable to set your wallpaper !!", Toast.LENGTH_LONG).show();
                                    }


                                } catch (IOException e) {
                                    Toast.makeText(getActivity(), "Something went wrong !!", Toast.LENGTH_LONG).show();
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onBitmapFailed(Exception e, Drawable errorDrawable) {

                            }

                            @Override
                            public void onPrepareLoad(Drawable placeHolderDrawable) {

                            }
                        });
Savoury answered 21/3, 2021 at 10:26 Comment(1)
Please check this, hope it will help you. #33178761Edger
M
2

hey I have some search and I have found a solution please follow

https://www.geeksforgeeks.org/how-to-set-an-image-as-wallpaper-programmatically-in-android/

Programmatically set android phone's background

Mooring answered 3/4, 2021 at 11:50 Comment(1)
Thanks @Mooring for answering but The answer is not related to my question.Savoury
H
2

Using glide

               Glide.with(context)
                .asBitmap().load(imageurl)
                .listener(object : RequestListener<Bitmap> {
                    override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: com.bumptech.glide.request.target.Target<Bitmap>?,
                            isFirstResource: Boolean
                    ): Boolean {
                        return false
                    }

                    override fun onResourceReady(
                            resource: Bitmap?,
                            model: Any?,
                            target: com.bumptech.glide.request.target.Target<Bitmap>?,
                            dataSource: DataSource?,
                            isFirstResource: Boolean
                    ): Boolean {

                        val wallpaperManager = WallpaperManager.getInstance(context)
                        try {
                            resource?.let {
                                wallpaperManager.setBitmap(it)
                            }

                        } catch (e: Exception) {
                            e.printStackTrace()
                        }

                        return false
                    }
                }).submit()
Hanky answered 17/11, 2021 at 6:56 Comment(0)
M
0

Did you somehow initialize progressDialog multiple times? If yes please initialize once and edit use whenever you need.

ProgressDialog progressDialog = new ProgressDialog(this); // Initialize once
progressDialog.setTitle("Please wait...");// can edit
progressDialog.setMessage("Set as your wallpaper...");// can edit
progressDialog.setCancelable(true);// can edit
progressDialog.setIndeterminate(false);// can edit
progressDialog.show();
progressDialog.dismiss();
Misuse answered 7/4, 2021 at 19:55 Comment(1)
Thanks @ashik, But my question is how to set wallpaper in android using Glide. Right now i used Picasso and its having some difficultiesSavoury

© 2022 - 2024 — McMap. All rights reserved.