title: How to get the run time image id to set wallpaper
body: <p>I am using <code>view paper</code> for image gallery, able to see all the images on swipe.
  Here I want to set the image as wallpaper by clicking the "Set as wallpaper" button.</p>
  
  <h2>Below are the difficulties I am facing:</h2>
  
  <p>I am able to set the image as wallpaper successfully, but with constant image 
  Ex: R.drawable.picture3.But at run time when the images are loaded at each 
  turn different image will be displayed so  cannot give this constant value 
  <code>R.drawable.picture3</code>.</p>
  
  <p>How do I get the run time image id which is displayed?
  Trying to achieve on click "Set as wallpaper" should set the current image
  as wallpaper.
  Note : v.getId()=R.drawable.picture1 not worked here both gave different value</p>
  
  <p>Below is my code:</p>
  
  <pre><code>             Context context;
           Integer[] imageIDs = {
           R.drawable.picture1,
           R.drawable.picture2,
           R.drawable.picture3,
           R.drawable.picture4,
           R.drawable.picture5,
  
           }; 
  
          @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.gallery);
               ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
               ImageAdapter adapter = new ImageAdapter(this);
               viewPager.setAdapter(adapter);
               Button button=(Button) findViewById(R.id.button1);
               button.setOnClickListener(new OnClickListener() 
                  {
  
                      @Override
                      public void onClick(View v) {
  
                           WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
                           try {
                              wallpaperManager.setResource(R.drawable.picture3);
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                          Toast.makeText(getApplicationContext(), 
                                  "Image is clicked-"+v.getBackground(), Toast.LENGTH_SHORT).show();
  
                      }
                  });        
  
          }
  
  
          public class ImageAdapter extends PagerAdapter{
          Context context;
          int currentPosition;
          private int[] GalImages = new int[] {
                   R.drawable.picture1,
                   R.drawable.picture2,
                   R.drawable.picture3,
                   R.drawable.picture4,
                   R.drawable.picture5,
          };
          ImageAdapter(Context context){
              this.context=context;
          }
          @Override
          public int getCount() {
              return GalImages.length;
          }
  
          @Override
          public boolean isViewFromObject(View view, Object object) {
              return view == ((ImageView) object);
          }
  
          @Override
          public Object instantiateItem(ViewGroup container, int position) {
              ImageView imageView = new ImageView(context);
              int padding = context.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_content_inset_material);
              imageView.setPadding(padding, padding, padding, padding);
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageView.setImageResource(GalImages[position]);
              ((ViewPager) container).addView(imageView, 0);
              return imageView;
          }
  
          @Override
          public void destroyItem(ViewGroup container, int position, Object object) {
              ((ViewPager) container).removeView((ImageView) object);
          }
  
          &lt;?xml version="1.0" encoding="utf-8"?&gt;
          &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" 
          &gt;
                  &lt;android.support.v4.view.ViewPager
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/pager"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_alignParentTop="true"&gt;
                  &lt;/android.support.v4.view.ViewPager&gt;
  
                  &lt;Button
                      android:id="@+id/button1"
                      android:layout_width="match_parent"
                      android:layout_height="30dp"
                      android:layout_alignParentBottom="true"
                      android:layout_alignParentLeft="true"
                      android:layout_alignParentStart="true"
                      android:background="#cccccc"
                      android:text="@string/set_as_wallpaper" /&gt;
  
          &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I don't want to achieve this in instantiateItem method in image adapter.</p>