J Doe
J Doe

Reputation: 33

How to set Title in Navigation drawer 2018

Hey guys new to all this and can't seem to figure a way to change the Navigation Title with my navigation drawer selection. Made a stock standard navigation drawer with android studio.

I feel it's really simple but hope someone will give me an answer.

i watch a youtube 2014 by Derek Banas and all he added in his fragment class was

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        ((MainActivity) activity).onSectionAttached(2);
    }

but that doesnt seem to work anymore. is there something similair for Studio 3.0.1?

Upvotes: 0

Views: 708

Answers (1)

Kunal Chawla
Kunal Chawla

Reputation: 1314

If you go to Android Studio, create New Project with the Navigation Drawer template, in MainActivity, you'll find the method onNavigationItemSelected. In that method, you can easily change activity title depending on your navigation option selection -

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
            setTitle("Camera");
        } else if (id == R.id.nav_gallery) {
            setTitle("Gallery");
        } else if (id == R.id.nav_slideshow) {
            setTitle("Slideshow");
        } else if (id == R.id.nav_manage) {
            setTitle("Manage");
        } else if (id == R.id.nav_share) {
            setTitle("Share");
        } else if (id == R.id.nav_send) {
            setTitle("Send");
        }

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

Upvotes: 1

Related Questions