Programmatically set android phone's background
Asked Answered
L

2

39

I'd like to allow the user to choose a background from a list of images, the user clicks on one of them and that image is used as background for his phone. My app should simply be another version of the android default gallery.

Is it possible to programmatically set the phone's wallpaper?

Levy answered 18/11, 2013 at 17:25 Comment(0)
B
68

First one, you need to set the permission in your Manifest.xml file

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

And you can set the background with this:

Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(R.drawable.five);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
}});
Buy answered 18/11, 2013 at 17:41 Comment(3)
Thanks! <uses-permission android:name="android.permission.SET_WALLPAPER" /> (with slash) to be exact.Fiore
What about if i have a url image source?Divergence
@FernandoUrban that is another question out of this topic. Specifically this question ask "I'd like to allow the user to choose a background from a list of images". I recommend you look in a separated thread.Buy
T
13

You can set a wallpaper using the WallpaperManager class. For example:

WallpaperManager wallpaperManager =
        WallpaperManager.getInstance(getApplicationContext());
wallpaperManager.setBitmap(someBitmap);
Two answered 18/11, 2013 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.