0

I have created a gradient drawable in the drawable folder. It is a shape in an xml file.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle" >
            <gradient
                android:angle="45"
                android:centerColor="@color/centerColor"
                android:endColor="@color/endColor"
                android:startColor="@color/startColor" />
        </shape>
    </item>
</selector>

I am trying to set the gradient as the wallpaper for the device using the app when a certain button is clicked. Is this even possible?

Thanks in advance. :)

Java Code:

public void onClick(View v) {
//                WallpaperManager myWallpaperManager  = WallpaperManager.getInstance(getApplicationContext());
                InputStream is = getResources().openRawResource(R.raw.gradient);
                Bitmap b = BitmapFactory.decodeStream(is);

                try {
//                    myWallpaperManager.setBitmap(+ R.drawable.gradient);
                    getApplicationContext().setWallpaper(b);
                    Toast.makeText(getApplicationContext(),"Wallpaper updated", Toast.LENGTH_SHORT ).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

1 Answers1

1

Is this even possible?

Yes, it is possible.

  1. Include this permission in your manifest.

  1. Create raw folder in your res folder and add that gradient inside the raw folder

  2. And use this code in your application.

           WallpaperManager myWallpaperManager  = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(R.raw.your_gradient_drawable);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
sushildlh
  • 8,986
  • 4
  • 33
  • 77