0

I am trying to follow a few different tutorials to have a ViewPager that lets a user swipe through images and then set the currently viewed image as their wallpaper. so far I have not been successful in getting the wallpaper set, and there is an issue with performance as the user swipes through the photos, performance degrades dramatically.

public class MainActivity extends AppCompatActivity {
ViewPager mViewPager;
int[] mImages = {R.drawable.example__1_, R.drawable.example__2_, R.drawable.example__3_,
        R.drawable.example__4_, R.drawable.example__5_, R.drawable.example__6_,
        R.drawable.example__7_, R.drawable.example__8_, R.drawable.example__9_,
        R.drawable.example__10_, R.drawable.example__11_, R.drawable.example__12_};
item mViewPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mViewPager = (ViewPager) findViewById(R.id.viewPagerMain);
    mViewPagerAdapter = new item(MainActivity.this, mImages);
    mViewPager.setAdapter(mViewPagerAdapter);
    final int set = mViewPager.getCurrentItem();

    Button button = findViewById(R.id.SetWall);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,
                    "Setting Wallpaper... 1/2", Toast.LENGTH_SHORT).show();
            WallpaperManager wpm
                    = WallpaperManager.getInstance(getApplicationContext());
            try {
                Toast.makeText(MainActivity.this,
                        "Setting Wallpaper... 2/2", Toast.LENGTH_SHORT).show();
                wpm.setResource(set);
                Toast.makeText(MainActivity.this,
                        "Wallpaper Set!", Toast.LENGTH_SHORT).show();
            }
            catch (IOException e) {
                Toast.makeText(MainActivity.this,
                        "Error", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
Alexander
  • 13
  • 3

1 Answers1

0
  1. Have you set the permission in your Manifest.xml file like this?

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    
  2. This is because your pages get destroyed and recreated every time they get off screen which happens when you swipe to the next.

    To make ViewPager smoother use mViewPager.setOffscreenPageLimit(NumberOfPages-1); after you set the adapter (mViewPager.setAdapter();)

Paulus
  • 162
  • 7
  • Thank you, the ViewPager is a lot smoother now thanks! – Alexander Mar 22 '21 at 13:19
  • But I already had the uses-permission line added to my manifest and the problem persists, i.e. it does not actually set the wallpaper? any other ideas would be greatly appricated. – Alexander Mar 22 '21 at 13:20
  • Instead of `wpm.setResource(set);` try `wpm.setResource(this.mImages[set]);`. `mViewPager.getCurrentItem();` returns an index and `wpm.setResource()` takes a bitmap as parameter. – Paulus Mar 22 '21 at 19:38