Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:54:05+00:00 2026-06-12T15:54:05+00:00

I have a ViewPager that loads images from a remote server. My problem is,

  • 0

I have a ViewPager that loads images from a remote server. My problem is, I don’t know how to give it a function once a specific position is selected (not clicked). Let’s say, I swipe the view pager until it reaches the specific image like image number 5, I want to show some Toast saying “image 5”.

pager.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {  





 public class ImagePagerActivity extends BaseActivity {

    private ViewPager pager;

    private DisplayImageOptions options;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_pager);
    Bundle bundle = getIntent().getExtras();
    String[] imageUrls = bundle.getStringArray(Extra.IMAGES);
    int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);

    options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.image_for_empty_url)
                .cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .build();

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new ImagePagerAdapter(imageUrls));
    pager.setCurrentItem(pagerPosition);

    }

    protected void onStop() {
    imageLoader.stop();
    super.onStop();
    }

    private class ImagePagerAdapter extends PagerAdapter {

    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
    this.images = images;
    inflater = getLayoutInflater();
    }

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

    public void finishUpdate(View container) {
    }

    public int getCount() {
    return images.length;
    }

    public Object instantiateItem(View view, int position) {
                final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
                final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
                final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);

    imageLoader.displayImage(images[position], imageView, options, new ImageLoadingListener() {

    public void onLoadingStarted() {
    spinner.setVisibility(View.VISIBLE);
    }


    public void onLoadingFailed(FailReason failReason) {
    String message = null;
    switch (failReason) {
    case IO_ERROR:
    message = "Input/Output error";
    break;
    case OUT_OF_MEMORY:
    message = "Out Of Memory error";
    break;
    case UNKNOWN:
    message = "Unknown error";
    break;
    }
    Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

    spinner.setVisibility(View.GONE);
                              imageView.setImageResource(android.R.drawable.ic_delete);
                    }


    public void onLoadingComplete(Bitmap loadedImage) {
    spinner.setVisibility(View.GONE);
    Animation anim = AnimationUtils.loadAnimation(ImagePagerActivity.this, R.anim.fade_in);
    imageView.setAnimation(anim);
    anim.start();
    }

    public void onLoadingCancelled() {
    // Do nothing
    }
    });

    ((ViewPager) view).addView(imageLayout, 0);
    return imageLayout;
    }

    public boolean isViewFromObject(View view, Object object) {
    return view.equals(object);
    }

    public void restoreState(Parcelable state, ClassLoader loader) {
    }
    public Parcelable saveState() {
    return null;
    }

    public void startUpdate(View container) {
    }
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:54:06+00:00Added an answer on June 12, 2026 at 3:54 pm

    You can implement onPageChangeListener, Than you can set this pageChageListener to your ViewPager using (setOnPageChangeListener()) …

    public class ImagePagerActivity extends BaseActivity implements onPageChangeListener {
        private ViewPager pager;
        // other variables here
    
        public void onCreate(Bundle savedInstanceState) {
            // create view Pager 
            // other variables 
            pager.setOnPageChangeListener(this);
        }
    
        // methods for Page Change Listener
        @Override
        public void onPageSelected(int pos) {
            if (pos==5) {
              //show Toast here 
            }
        }
    
        @Override
        public void onPageScrollStateChanged(int arg0) {
            // do Nothing
        }
    
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // do Noting
        }
    
    }// class def ends here
    

    edit..

    Use this short hand version

    Pager.setOnPageChangeListener(new OnPageChangeListener() {
    
        @Override
        public void onPageSelected(int arg0) {
            if (pos==5) {
                //show Toast here 
            }
        }
    
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }
    
        @Overridee3public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm porting an application from WebForms to MVC. I have a WebForms UserControl that
I have a ViewPager on my Kindle Fire app that adds roughly 30 views
I have a ViewPager FragmentActivity that holds 3 tabs, Each one of the tabs
I have Activity with ViewPager that contains several Fragments. I am also using drop-down
I have a ViewPager hooked up to a FragmentPagerAdapter that's displaying three fragments. The
I have created an android app that uses a ViewPager to swipe through three
I have a ViewPager that has one ImageView per view. I need to setup
I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new
I have a simple ViewPager example that just places a TextView with the current
I have a ViewPager that uses ListFragments on each page. I implement onListItemClick and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.