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 9187133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:41:50+00:00 2026-06-17T19:41:50+00:00

I’ve been wrestling with this for quite a while now. I switched from having

  • 0

I’ve been wrestling with this for quite a while now.

I switched from having a regular Activity to now using fragments (by users recommendations), but I’ve been getting the same issue.

I have 3 pages that are now 3 fragments inside of a ViewPager. I want the button with the id addSiteButton in the addSite fragment; when clicked, to scroll over to setCurrentItem(2).

Any help is appreciated! Here’s my code.

The FragmentActivity

public class fieldsActivity extends FragmentActivity {


private static final int NUMBER_OF_PAGES = 3;

ViewPager mPager;
MyFragmentPagerAdapter mMyFragmentPagerAdapter;


/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // to create a custom title bar for activity window
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.fields);
    // use custom layout title bar
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);

    mPager = (ViewPager) findViewById(R.id.fieldspager);
    mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mMyFragmentPagerAdapter);
    mPager.setCurrentItem(1);


}


private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int page) {

        switch (page) {

            case 0: return new settingFields();

            case 1: return new addSite();

            case 2: return new createSite();

        }

       return null;


    }

    @Override
    public int getCount() {

        return NUMBER_OF_PAGES;
    }

}

The class with the button

public class addSite extends Fragment {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    if (container == null) {
        return null;
    }

    RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
    final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
    Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
    addSiteButton.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {

            mPager.setCurrentItem(2, true);

        }
    });

   return mRelativeLayout;

}
}

I feel like I’m going in circles and not getting the desired motion.

Thanks for the help!

  • 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-17T19:41:51+00:00Added an answer on June 17, 2026 at 7:41 pm

    Simply make the Fragment as a class inside the FragmentActivity and take out the variable shadowing in onCreateView() since your pager is a global variable.

    public class fieldsActivity extends FragmentActivity {
    
    
      private static final int NUMBER_OF_PAGES = 3;
    
      ViewPager mPager;
      MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    
    
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // to create a custom title bar for activity window
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    
        setContentView(R.layout.fields);
        // use custom layout title bar
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);
    
        mPager = (ViewPager) findViewById(R.id.fieldspager);
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mMyFragmentPagerAdapter);
        mPager.setCurrentItem(1);
    
    
      }
    
      public static class addSite extends Fragment {
    
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
          RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
          //final ViewPager mPager = (ViewPager) mRelativeLayout.findViewById(R.id.fieldspager);
          Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
          addSiteButton.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
            mPager.setCurrentItem(2, true);
    
            }
          });
    
          return mRelativeLayout;
    
        }
      }
    }
    

    Also consider using the @Override annotation whenever you override a method so you don’t make mistakes and use proper naming conventions. Java classes start with a capital.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am using jsonparser to parse data and images obtained from json response. When

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.