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

  • Home
  • SEARCH
  • 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 8276453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:22:49+00:00 2026-06-08T08:22:49+00:00

What my code does: Here’s my code which for viewpager that swipes between xml

  • 0

What my code does:

Here’s my code which for viewpager that swipes between xml layouts (named left.xml, right.xml and center.xml).

What I want it to do

I want to swipe between images (stored in the drawable folder). When I replace R.layout.xml with R.drawable.image, my app crashes down. Can anybody help me figure it out?

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.left;
            break;
        case 1:
            resId = R.layout.center;
            break;
        case 2:
            resId = R.layout.right;
            break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}

}

  • 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-08T08:22:50+00:00Added an answer on June 8, 2026 at 8:22 am

    If you are using the same implementation and just adding R.drawable.XXX instead of R.layout.YYY then the problem is right there. You are using LayoutInflater to inflate ImageViews, the layout inflater as it states by itself it inflates a whole layout in a single View.

    Instead of doing that try to create ImageView objects through code and then in the return return the newly created ImageView. Some sample code would be:

    public Object instantiateItem(View collection, int position) {
    
        ImageView img = new ImageView(context); //this is a variable that stores the context of the activity
        //set properties for the image like width, height, gravity etc...
    
        int resId = 0;
        switch (position) {
            case 0:
                resId = R.drawable.img1;
                break;
            case 1:
                resId = R.drawable.img2;
                break;
            case 2:
                resId = R.drawable.img3;
                break;
        }
    
        img.setImageResource(resId); //setting the source of the image
        return img;
    }
    

    If you are just using a specific amount of images or pages you should consider adding them in the xml that contains the ViewPager rather than dynamically creating the ViewPager.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code does an animation that flips from right to left. The problem is
I have a piece of code here that does not work despite me using
I am playing around with WPF a bit. Here is the Code that does
here is my code it shares the folder but that does not work correctly
The script is on jsfiddle here : CODE What it does at the moment
Here is my code, this does not work in Chrome, Safari, FF or Opera.
Here's the code, I don't quite understand, how does it work. Could anyone tell,
Why does the titlebar overlap the pixels of JPanel. Here some code: protected void
I've seen other questions here about PNS, in terms of code, but how does
This code does not throw an error but the query fails, that is, the

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.