Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Drawer on multiple Activities

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

like image 527
Deniz C. Avatar asked Sep 09 '13 12:09

Deniz C.


People also ask

How do I use the same navigation drawer in all activities?

Navigation drawers are the most common use android widget in android. The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.

How do I add a navigation drawer to an existing activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.


2 Answers

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

like image 145
Harish Godara Avatar answered Oct 13 '22 22:10

Harish Godara


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")
}
like image 21
Kevin van Mierlo Avatar answered Oct 13 '22 20:10

Kevin van Mierlo