Change the fragment of visual by a button Click


I'm trying to change the viewpager fragment by clicking on a button. I have 5 fragments, each fragment has it's own xml file (frag1.xml, frag2.xml, and so on). Every fragment has it's 5 buttons that should go to other pages of the viewpager. But the problem is how do I check in the FragmentPageAdapter which button is clicked and how to get there?

I'll show the code I have then it should be clear I think. Think of it like a homescreen that has dots at the bottom and I you click a certain dot, you'll go to the corresponding screen.


public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 6;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {

    /** This method will be invoked when a page is requested to create */
    public Fragment getItem(int arg0) {


        case 0:

            return new Fragment1();

        case 1:
            return new Fragment2();

        case 2:
            return new Fragment3();

        case 3:
            return new Fragment4();

        case 4:
            return new Fragment5();

            return null;


    /** Returns the number of pages */
    public int getCount() {
        return PAGE_COUNT;

public class Frag1 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.frag_one, container, false);

        OnClickListener changeFrag = new OnClickListener() {
            public void onClick(View v) {
            /*When I click this button in my fragment, I'd like it to go to fragment 3 for example*/

        ImageButton btnT = (ImageButton) v.findViewById(;

        return v;


public class MainActivity extends FragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);/** Getting a reference to the ViewPager defined the layout file */
        ViewPager pager = (ViewPager) findViewById(;

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */

       //pager.setPageTransformer(true, new ZoomOutPageTransformer());

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

Is something like this possible? Can someone help me in the right direction please?

I'm not sure what exactly you said about "Change".

If you're asking about change page display index, you can try

ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);

Or, if you're asking about change content, here's what I did

public class MyPagerAdapter extends FragmentStatePagerAdapter
  private FragmentManager fragMan;
  private ArrayList<Fragment> fragments=new ArrayList<Fragment>();

  public void clearAll() //You can clear any specified page if you want...
    for(int i=0; i<fragments.size(); i++)
    fragments=new ArrayList<Fragment>();

  public void addList() //Add some new fragment...
    listFrag=new ListFragment();

Hope it helps~