Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the first item in a navigation drawer and open a fragment on application start

I have created MainActivity with NavigationView. When Activity is opened I want to automatically select the first item in the navigation drawer and open Fragment under that item. I've searched a lot but didn't find any proper solutions.

What is the proper way to do this ?

Main Activity:

public class MainActivity extends AppCompatActivity implements Config {

private NavigationView navigationView;
private DrawerLayout drawerLayout;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    toolbar.setTitleTextColor(getResources().getColor(R.color.colorIcons));

    if (null != getSupportActionBar())
        getSupportActionBar().setLogo(R.drawable.ic_blogger_white);

    //Start PostListFragmentWebView
    /*PostListFragmentWebView postListFragmentWebView = new PostListFragmentWebView();
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame, postListFragmentWebView)
            .commit();*/

    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigationView);

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {


            //Checking if the item is in checked state or not, if not set it to checked state.
            if (menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            //Check to see which item was clicked and perform the appropriate action.
            switch (menuItem.getItemId()) {

                case R.id.posts:
                    PostListFragmentWebView postListFragment = new PostListFragmentWebView();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.frame, postListFragment)
                            .commit();
                    return true;

                case R.id.pages:
                    PageListFragmentWebView pagetListFragment = new PageListFragmentWebView();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.frame, pagetListFragment)
                            .commit();
                    return true;

                case R.id.blog:
                    BlogInfoFragmentWebView blogInfoFragment = new BlogInfoFragmentWebView();
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.frame, blogInfoFragment)
                            .commit();
                    return true;

                default:
                    Toast.makeText(getApplicationContext(), getResources().getString(R.string.drawer_error), Toast.LENGTH_SHORT).show();
                    return true;

            }

        }
    });

    // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank.
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank.

            super.onDrawerOpened(drawerView);
        }
    };

    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    drawerLayout.getChildAt(0).setSelected(true);

    //calling sync state is necessary or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();
}
}
like image 732
Darpan Kulkarni Avatar asked Jun 25 '15 06:06

Darpan Kulkarni


2 Answers

In onCreate(), following code will load the first item's fragment upon first start:

if (savedInstanceState == null) {
    navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
}

Thanks to calvinfly for this comment.

like image 188
Darpan Kulkarni Avatar answered Oct 05 '22 19:10

Darpan Kulkarni


Add android:checked="true" to your first menu item.

And manually select one item, using

getSupportFragmentManager().beginTransaction().replace(R.id.frame, postListFragment).commit();

to open fragment.

like image 34
Ellie Zou Avatar answered Oct 05 '22 19:10

Ellie Zou