title: How do i Swipe selected grid image in Full Screen
body: <p>i created a grid-view image app, and i want to show selected image in image swipe.</p>
  
  <p>actually i implemented image swipe in my app <strong>but</strong> the problem is image swipe starts from 1st image not from selected image.</p>
  
  <p><strong>example:</strong></p>
  
  <p>if i choose 3rd image image swipe should starts from 3rd image not from 1st image.</p>
  
  <p><strong>my code:</strong></p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.td.gridview;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.ViewGroup.LayoutParams;
  import android.view.ViewTreeObserver;
  import android.widget.AdapterView;
  import android.widget.BaseAdapter;
  import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  import android.widget.AdapterView.OnItemClickListener;
  
  public class MainActivity extends Activity {
  
      private GridView photoGrid;
      private int mPhotoSize, mPhotoSpacing;
      private ImageAdapter imageAdapter;
  
      // Some items to add to the GRID
      private static final String[] CONTENT = new String[] 
              { 
              "pic1",
              "pic2",
              "pic3",
              "pic4",
              "pic5",
              "pic6",
              "pic7",
              "pic8",
              "pic9"
              };
      static final int[] ICONS = new int[] 
              { 
              R.drawable.pic1,
              R.drawable.pic2,
              R.drawable.pic3,
              R.drawable.pic4,
              R.drawable.pic5,
              R.drawable.pic6,
              R.drawable.pic7,
              R.drawable.pic8,
              R.drawable.pic9
              };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // get the photo size and spacing
          mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
          mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);
  
          // initialize image adapter
          imageAdapter = new ImageAdapter();
  
          photoGrid = (GridView) findViewById(R.id.albumGrid);
  
          //start sent image to full screen             
  
          /**
           * On Click event for Single Gridview Item
           * */
          photoGrid.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View v,
                      int position, long id) {
  
                  // Sending image id to FullScreenActivity
                  Intent i = new Intent(getApplicationContext(), SwipeActivity.class);
                  // passing array index
                  i.putExtra("id", position);
                  startActivity(i);
              }
          });
          //end sent image to full screen
  
          // set image adapter to the GridView
          photoGrid.setAdapter(imageAdapter);
  
          // get the view tree observer of the grid and set the height and numcols dynamically
          photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
              @Override
              public void onGlobalLayout() {
                  if (imageAdapter.getNumColumns() == 0) {
                      final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                      if (numColumns &gt; 0) {
                          final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                          imageAdapter.setNumColumns(numColumns);
                          imageAdapter.setItemHeight(columnWidth);
  
                      }
                  }
              }
          });
      }
  
      // ///////// ImageAdapter class /////////////////
      public class ImageAdapter extends BaseAdapter {
          private LayoutInflater mInflater;
          private int mItemHeight = 0;
          private int mNumColumns = 0;
          private RelativeLayout.LayoutParams mImageViewLayoutParams;
  
          public ImageAdapter() {
              mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                      LayoutParams.MATCH_PARENT);
          }
  
          public int getCount() {
              return CONTENT.length;
          }
  
          // set numcols
          public void setNumColumns(int numColumns) {
              mNumColumns = numColumns;
          }
  
          public int getNumColumns() {
              return mNumColumns;
          }
  
          // set photo item height
          public void setItemHeight(int height) {
              if (height == mItemHeight) {
                  return;
              }
              mItemHeight = height;
              mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
              notifyDataSetChanged();
          }
  
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          public View getView(final int position, View view, ViewGroup parent) {
  
              if (view == null)
                  view = mInflater.inflate(R.layout.photo_item, null);
  
              ImageView cover = (ImageView) view.findViewById(R.id.cover);
              TextView title = (TextView) view.findViewById(R.id.title);
  
              cover.setLayoutParams(mImageViewLayoutParams);
  
              // Check the height matches our calculated column width
              if (cover.getLayoutParams().height != mItemHeight) {
                  cover.setLayoutParams(mImageViewLayoutParams);
              }
  
              cover.setImageResource(ICONS[position % ICONS.length]);
              title.setText(CONTENT[position % CONTENT.length]);
  
              return view;
          }
      }
  
  }
  </code></pre>
  
  <p><strong>SwipeActivity.java</strong></p>
  
  <pre><code>    package com.td.gridview;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import com.td.gridview.MainActivity;
  
  
  
  public class SwipeActivity extends Activity 
  {   
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.swipe_view);
  
      int[] icons = MainActivity.ICONS;    
  
      //get image position
  
      Intent n = getIntent();
      int position= n.getIntExtra("id", position);
  
      ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);    
      ImagePagerAdapter adapter = new ImagePagerAdapter(SwipeActivity.this , MainActivity.ICONS[position]);
      viewPager.setAdapter(adapter);
      viewPager.setCurrentItem(position);
    }
  
    private class ImagePagerAdapter extends PagerAdapter 
    {
        int[] icons = MainActivity.ICONS;    
  
      @Override
      public int getCount() 
      {
        return icons.length;
      }
  
      @Override
      public boolean isViewFromObject(View view, Object object) 
      {
        return view == ((ImageView) object);
      }
  
      @Override
      public Object instantiateItem(ViewGroup container, int position) 
      {
        Context context = SwipeActivity.this;
        ImageView imageView = new ImageView(context);
  //      int padding = context.getResources().getDimensionPixelSize(
  //          R.dimen.padding_large);
  //      imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageResource(icons[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
      }
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) 
      {
        ((ViewPager) container).removeView((ImageView) object);
      }
    }
  }
  </code></pre>