Question

I'm using the PhotoView library to have a slide view of images. That's easy. Then I wanted set as wallpaper the image I wanted, maybe on click in a button but I have a problem! The library seems that not allows create a layout but only its in this way.

<com.ex.paper.HackyViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" />

So i can't add any button. I need detect the right array position and in some way set that image as wallpaper.. So far the code is this:

`public class MainActivity extends Activity { Bitmap bitmap; int lastImageRef; private static final String ISLOCKED_ARG = "isLocked";

private ViewPager mViewPager;
private static MenuItem menuLockItem;
private WallpaperManager wallpaper;

private static int[] sDrawables = { R.drawable.wallpapertwo, R.drawable.twixkatfirst, R.drawable.wallpaper,
    R.drawable.sfondo, R.drawable.wallpapertre};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
    setContentView(mViewPager);

    mViewPager.setAdapter(new SamplePagerAdapter());

    if (savedInstanceState != null) {
        boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false);
        ((HackyViewPager) mViewPager).setLocked(isLocked);
    }

}

static class SamplePagerAdapter extends PagerAdapter {


    @Override
    public int getCount() {
        return sDrawables.length;
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());
        photoView.setImageResource(sDrawables[position]);
        WallpaperManager wallpaper = WallpaperManager.getInstance(container.getContext());
        /*Toast number = Toast.makeText(container.getContext(), "wallpaper number "+position, Toast.LENGTH_LONG);
        number.show();*/

        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        try
        {
            wallpaper.setResource(sDrawables[position]);
        }
        catch (IOException e)
        {

        }
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}  

}`

Now, in the try catch, I can set the wallpaper through position but without tapping any button!! And of course it's not a good way. Any solution? Maybe a button in the actionbar? But I can't find the array position at that point.

Was it helpful?

Solution

You can use FrameLayout:

<FrameLayout...>
    <com.ex.paper.HackyViewPager/>
    <Button/>
<FrameLayout/>

In onCreate:

    setContentView(your.layout.with.frameLayout);
    mViewPager = (HackyViewPager) findViewById(R.id.view_pager);
    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
               wallpaper.setResource(sDrawables[mViewPager.getCurrentItem()]);                  
            }
        });
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top