Search code examples
androidwallpaper

Setting wallpaper using onclick event of a button has no effect


I'm following the tutorial trying to set the wallpaper using onclick event of a button.

My code for that is,

private OnClickListener startListener = new OnClickListener(){

    public void onClick(View v){

        setBg();}

    public void setBg(){
        WallpaperManager myWallpaperManager
         = WallpaperManager.getInstance(getApplicationContext());
        try {
         myWallpaperManager.setResource(R.drawable.shrek);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

       }

Also i've set the permission SETWALLPAPER.

But when i click, Nothing happens.

whats the problem here?


Solution

  • In this tutorial, he uses the OnClickListener a bit different:
    buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){....

    Try this, should work:

    start.setOnClickListener(new Button.OnClickListener(){
    
       @Override
       public void onClick(View arg0) { 
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
         myWallpaperManager.setResource(R.drawable.shrek);
        } catch (IOException e) {
         e.printStackTrace();
        }
       }});
    

    EDIT: Of course you can also just call your method in OnClick instead of handling it directly