Q:

how to show tab of view pager on click of navigation drawer item without overlaping?

There are three fragments in my app.I showed them in the tabs successfully.Now i want to show fragment of tab1 on the click of navigationdrawer item1 and same for the rest.But there is a problem of overlapping.Can anyone guide me to get rid of this problem?
MainActivity.java
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    // Set the ViewPagerAdapter into ViewPager
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = 
    fragmentManager.beginTransaction();
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        fragmentOne f1 = new fragmentOne();
        fragmentTransaction.replace(R.id.fragment_container,f1);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("Camera");

    } else if (id == R.id.nav_gallery) {
        fragmentTwo f2 = new fragmentTwo();
        fragmentTransaction.replace(R.id.fragment_container,f2);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("Gallery");

    } else if (id == R.id.nav_slideshow) {
    MenuFragment f3 = new MenuFragment();
        fragmentTransaction.replace(R.id.fragment_container,f3);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("List");

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout)     
    findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

FragmentOne.java
  public class fragmentOne extends Fragment {
TextView t1;
public fragmentOne(){

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    return inflater.inflate(R.layout.fragment_one_layout,container, false);
}
}

ViewPagerAdapter.java
   public class ViewPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3" };
Context context;

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

@Override
public int getCount() {
    return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
    switch (position) {

        // Open FragmentTab1.java
        case 0:
            fragmentOne fragmenttab1 = new fragmentOne();
            return fragmenttab1;

        // Open FragmentTab2.java
        case 1:
            fragmentTwo fragmenttab2 = new fragmentTwo();
            return fragmenttab2;

        // Open FragmentTab3.java
        case 2:
            MenuFragment fragmenttab3 = new MenuFragment();
            return fragmenttab3;
    }
    return null;
}

@Override
public CharSequence getPageTitle(int position) {
    return tabtitles[position];
}
}

MainActivity.xml
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android=
 "http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.chaitanya.navbar.MainActivity"
tools:showIn="@layout/app_bar_main">
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:textColor="#000000" />

</android.support.v4.view.ViewPager>

<RelativeLayout

    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center|center_horizontal"
    ></RelativeLayout>

A:

Is there a reason you are using a FragmentTransaction? You could call
viewPager.setCurrentItem(0);

The argument you pass (0 or 1 or 2 or ...) is based on which action you click in the NavigationView. The tabs should update automatically when the ViewPager changes.

