17

Is there a way to configure only one time the Navigation Drawer, and the display it on multiple Activites?

Deniz Celebi
  • 888
  • 4
  • 14
  • 24

2 Answers2

29

For this just create a BaseActivity class that implements the drawer, and let all your other activities extend this one.

Harish Godara
  • 2,388
  • 1
  • 14
  • 28
  • @Harish Godara: Can u give any example or any tutorial for the same – Sumit Patel Nov 20 '13 at 11:23
  • @Harish Godara: I was trying the same example but can u please helpme that when i made a test project it works fine but when i integrated the same in my application the drawer didn't get opens why that so ..? – Sumit Patel Nov 21 '13 at 08:12
  • @HarishGodara I got it. can you please tell that if i am having different activities says A,B,C,D now I am showing drawer on a and wants to navigate to B along with the same drawer and it's state without using fragments – Sumit Patel Nov 21 '13 at 11:19
  • 1
    I am doing something close to this, but I am running into some small but irritating issues. First, when you launch a new activity from the nav drawer it looks kind of clunky because the new activity is firing at the same time as the drawer closes. Also, when you return to the previous activity, there is a title swap that occurs briefly. The swap is the change from global to activity context `onDrawerClosed`, which didn't get a chance to fire before the new activity launched. – theblang Mar 31 '14 at 21:53
  • 1
    All things worked perfect. But how can I show different layout to different child activities???? Please provide any information for this. – Uniruddh Apr 12 '14 at 11:34
  • This will still work with NavigationDrawer and new Toolbar? cause for me new Toolbar is making troubles with this approach (crashes because 'android.support.v7.widget.Toolbar.getTitle()' is called on a null object reference). – Ewoks Feb 09 '16 at 10:14
7

For people wanting an code example with Activities, take a look at my answer over here: https://stackoverflow.com/a/19451842/2767703

If you want a nice transition I would suggest this: When you click on an item in the NavigationDrawer close the navigation drawer and simultaneously use postdelayed with 250 (time it takes to close the NavigationDrawer). Also simultaneously animate the main content's alpha to 0 with 150 milliseconds. Then when an Activity starts animate the main content's alpha to 1 with 250 milliseconds. This gives a great transition. I found it in the Google IO code: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java

By the way, you should also look at the link @Harish Godara gave: http://www.michenux.net/android-navigation-drawer-748.html It works with Fragments but it has a nice way of implementing the NavigationDrawer.

Edit

Since some links are dead here is what I used in my last project to get the animation. It is in Kotlin, but it should make the point clear. This is all code from the BaseDrawerActivity:

private val NAVDRAWER_LAUNCH_DELAY = 250L
private val MAIN_CONTENT_FADEOUT_DURATION = 150L
private val MAIN_CONTENT_FADEIN_DURATION = 250L

-

private var shouldAnimate:Boolean
    set(value) { intent.putExtra("animateTransition", value) }
    get() = intent.getBooleanExtra("animateTransition", false)

-

private fun changeDrawerItem(newClass: Class<*>) {
    runDelayed(NAVDRAWER_LAUNCH_DELAY, {
        startActivity(Intent(this, newClass).apply {
            addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            putExtra("animateTransition", true)
            putExtra("selectedNav", selectedNavigationItem.name)
        })
        overridePendingTransition(0, 0)
    })

    mainContent.animate()?.alpha(0f)?.duration = MAIN_CONTENT_FADEOUT_DURATION
}

-

override fun onStart() {
    super.onStart()

    if(shouldAnimate) {
        mainContent.alpha = 0f
        mainContent.animate()?.alpha(1f)?.duration = MAIN_CONTENT_FADEIN_DURATION
    } else {
        mainContent.alpha = 1f
    }

    val selectedNav = intent.getStringExtra("selectedNav")
    if(selectedNav != null) {
        selectedNavigationItem = DrawerItem.valueOf(selectedNav)
    }
}

-

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    setIntent(intent)

    if(shouldAnimate) {
        overridePendingTransition(0, 0)
    }
}

-

override fun onResume() {
    super.onResume()
    intent.removeExtra("animateTransition")
}
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • the link leads to a 404 now, however im really curious about the nice transitions. Do you have some resources on this? – 最白目 Dec 12 '17 at 15:00
  • @dan I added an example how I did it in my last project. It is written in Kotlin, but should be enough to give you an idea how to do this – Kevin van Mierlo Dec 13 '17 at 09:21