2

Hi I have implemented a Tab Layout using View Pager. I have seven fragment implemented say Frag1, Frag 2...Frag 7 corresponding to Tab 1, Tab 2....Tab7 . I have also implemented a Drawer Layout. Navigation also has corresponding 7 items My requirement is as below:

When I am in Tab 1 and pull the drawer out, the first item in the navigation drawer must be highlighted. When I am in Tab 5, 5th item must be highlighted and so on.

I tried doing this seeing number of tutorials but was unable to accomplish.

My activity_main.xml

<android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabMode="scrollable"
                app:tabTextAppearance="@style/TabTextAppearance"/>
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navheader"
        app:menu="@menu/menu_navigation" />

My Main Activity.Java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Adding Toolbar to Main screen
        mHandler = new Handler();
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Setting ViewPager for each Tabs
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        // Set Tabs inside Toolbar
        TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        // Create Navigation drawer and inlfate layout
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        // Adding menu icon to Toolbar
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            VectorDrawableCompat indicator =
                    VectorDrawableCompat.create(getResources(), R.drawable.ic_menu, getTheme());
            indicator.setTint(ResourcesCompat.getColor(getResources(), R.color.white, getTheme()));
            supportActionBar.setHomeAsUpIndicator(indicator);
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        // Set behavior of Navigation drawer
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    // This method will trigger on item Click of navigation menu

//                    public boolean onNavigationItemSelected(MenuItem menuItem) {
//                        // Set item in checked state
//                        menuItem.setChecked(true);
//
//                        // TODO: handle navigation
//
//                        // Closing drawer on item click
//                        mDrawerLayout.closeDrawers();
//                        return true;
//                    }


                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {

                        //Check to see which item was being clicked and perform appropriate action
                        switch (menuItem.getItemId()) {
                            //Replacing the main content with ContentFragment Which is our Inbox View;
                            case R.id.nav_home:
                                navItemIndex = 0;
                                CURRENT_TAG = TAG_HOME;
                                break;
                            case R.id.nav_history:
                                navItemIndex = 1;
                                CURRENT_TAG = TAG_HISTORY;
                                break;
                            case R.id.nav_location:
                                navItemIndex = 2;
                                CURRENT_TAG = TAG_LOCATION;
                                break;
                            case R.id.nav_developments:
                                navItemIndex = 3;
                                CURRENT_TAG = TAG_DEVELOPMENTS;
                                break;
                            case R.id.nav_donations:
                                navItemIndex = 4;
                                CURRENT_TAG = TAG_DONATIONS;
                                break;
                            case R.id.nav_resources:
                                navItemIndex = 5;
                                CURRENT_TAG = TAG_RESOURCES;
                                break;
                            case R.id.nav_feedback:
                                navItemIndex = 6;
                                CURRENT_TAG = TAG_FEEDBACK;
                                break;
                            case R.id.nav_contactus:
                                navItemIndex = 7;
                                CURRENT_TAG = TAG_CONTACTUS;
                                break;

                            default:
                                navItemIndex = 0;
                        }

                        //Checking if the item is in checked state or not, if not make it in checked state
                        if (menuItem.isChecked()) {
                            menuItem.setChecked(false);
                        } else {
                            menuItem.setChecked(true);
                        }
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        viewPager.setCurrentItem(navItemIndex);

                        return true;
                    }
                });

Kindly help me in achieving this.

The AV
  • 129
  • 2
  • 16

1 Answers1

2

use Tablayout.addOnTabSelectedListener

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
         int position = tab.getPosition();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
});

From this code you will get the selected tab position.

Now use the position value in NavigationView and check that item using index

navigationView.getMenu().getItem(position).setChecked(true);
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • When I am in tab 1 and open the drawer, first item is highlighted. That's fine. But when I swipe to second or third fragment or tab, still first remains highlighted. Instead it should have highlighted second or third item. Probably I have inserted navigationView.getMenu().getItem(position).setChecked(true); at a wrong place. Help me in this regard. – The AV Mar 28 '17 at 07:15
  • Basically every time when I open Drawer, I must be able to call tabLayout.addOnTabSelectedListener and then use navigationView.getMenu().getItem(position).setChecked(true); How can I do that? – The AV Mar 28 '17 at 07:20
  • take a look at this [Answer](http://stackoverflow.com/questions/26082467/android-on-drawer-closed-listener) – rafsanahmad007 Mar 28 '17 at 07:30
  • rafsanahmad007, Thank you. I am seeing multiple answers there. Just one more thing, can you help me with where exactly to put that piece of code. Can you update your answer along with few of my code? That would be great. – The AV Mar 28 '17 at 07:36
  • call this: `navigationView.getMenu().getItem(position).setChecked(true);` each time a `onTabSelected` is invoked,, – rafsanahmad007 Mar 28 '17 at 08:21