Skip to content

Dinesh2510/Navigation-Drawer-In-All-Activity

Repository files navigation

Navigation-Drawer-In-All-Activity

Create Toolbar And Navigation Drawer

//Toolbar Method

void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setTitle("Drawer News");
}

//Navigation Drawer Method

void initNavigationMenu() {
    NavigationView nav_view = (NavigationView) findViewById(R.id.nav_view);
    final 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) {
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    nav_view.setNavigationItemSelectedListener(this);

    // open drawer at start
    drawer.closeDrawer(GravityCompat.START);
}

👉 Create Toolbar & Navigation Drawer Method in MainActivity than extended the MainActivity in where u want to add the Navigation Drawer add following part to next activity.(concepts of polymorphism)

public class Main2Activity extends MainActivity {

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

    initToolbar();
    initNavigationMenu();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("Second Activity");
    }