MaterialDrawer

repository·develop·Indexed 11 days ago

https://github.com/mikepenz/materialdrawer

A flexible Android library for implementing Material Design navigation drawers. Built on RecyclerView, it supports Material 3 (v9.x), account switching via AccountHeaderView, custom drawer items, and integration with Android-Iconics for font icons. It includes support for Navigation Component and customizable styling through theme attributes like materialDrawerStyle.

Tokens
7.3K
Snippets
24
Records
27
Agent score
94%

What's inside MaterialDrawer

  1. Deciding when to use MaterialDrawer vs. Google's Support Library drawer

    develop

    Choose between MaterialDrawer and the standard Google Design Support Library drawer based on your required complexity:

    Use Google's Support Library drawer if:

    • You need a minimal drawer implementation.
    • You only require standard Main items.
    • You do not need profile functionality, custom items, custom styles, or advanced programmatic APIs.
    • You prefer to keep the implementation as simple as possible for better user experience.

    Use MaterialDrawer if you need advanced features such as:

    • Profile Switcher: Functionality to switch between user profiles.
    • Profile Page: A dedicated page for profile management.
    • Custom Items: Support for different item sizes, checkboxes, and other specialized UI elements.
    • High Customization: Extensive flexibility for custom styles and advanced programmatic control via the API.
  2. Create a custom MaterialDrawer style

    develop

    To customize the appearance of the drawer, create a custom style in your styles.xml that inherits from Widget.MaterialDrawerStyle. You can override specific attributes such as background colors, text colors, icon colors, and divider colors.

    After defining your custom drawer style, you must also define a header style (inheriting from a suitable parent) and then link both styles to your application's main theme using the materialDrawerStyle and materialDrawerHeaderStyle attributes.

    <!-- 1. Define a custom drawer style -->
    <style name="Widget.MaterialDrawerStyleCustom" parent="Widget.MaterialDrawerStyle">
        <item name="materialDrawerInsetForeground">#4000</item>
        <item name="materialDrawerBackground">?colorSurface</item>
        <item name="materialDrawerPrimaryText">@color/color_drawer_item_text</item>
        <item name="materialDrawerPrimaryIcon">@color/color_drawer_item_text</item>
        <item name="materialDrawerSecondaryText">@color/color_drawer_item_text</item>
        <item name="materialDrawerSecondaryIcon">@color/color_drawer_item_text</item>
        <item name="materialDrawerDividerColor">?colorOutline</item>
        <item name="materialDrawerSelectedBackgroundColor">?colorSecondaryContainer</item>
    </style>
    
    <!-- 2. Define a custom header style -->
    <style name="Widget.MaterialDrawerHeaderStyleCustom" parent="">
        <item name="materialDrawerCompactStyle">true</item>
        <item name="materialDrawerHeaderSelectionText">?colorOnSurface</item>
        <item name="materialDrawerHeaderSelectionSubtext">?colorOnSurface</item>
    </style>
    
    <!-- 3. Apply styles to your app theme -->
    <style name="SampleApp" parent="Theme.Material3.Light.NoActionBar">
        <item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyleCustom</item>
        <item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyleCustom</item>
    </style>
  3. Add MaterialDrawer to your XML layout

    develop

    The MaterialDrawerSliderView must be added as a child of a DrawerLayout. It acts as the slider content. Set its layout_gravity to start.

    <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <!-- Your main content goes here -->
        ...
    
        <com.mikepenz.materialdrawer.widget.MaterialDrawerSliderView
            android:id="@+id/slider"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
  4. Load images from URLs in MaterialDrawer

    develop

    MaterialDrawer does not include a built-in image loading library. To load images from URLs (e.g., for profile icons), you must initialize DrawerImageLoader with your preferred library (Picasso, Glide, etc.) before the first load occurs.

    // Initialize with Picasso
    DrawerImageLoader.init(object : AbstractDrawerImageLoader() {
        override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable) {
            Picasso.get().load(uri).placeholder(placeholder).into(imageView)
        }
    
        override fun cancel(imageView: ImageView) {
            Picasso.get().cancelRequest(imageView)
        }
    })
  5. Lock the MaterialDrawer

    develop

    Since MaterialDrawer wraps a standard DrawerLayout, you can control its locking behavior using the setDrawerLockMode method. This allows you to prevent the drawer from being opened or closed via standard gestures or interactions.

    drawerLayout.setDrawerLockMode(int lockMode); //or (int lockMode, int edgeGravity)
  6. Customize BezelImageView style

    develop

    You can overwrite the global style for the BezelImageView used within the MaterialDrawer. This allows you to control the mask drawable, shadow drawing, press selectors, and scale types for the image views within the drawer.

    <style name="BezelImageView">
        <item name="biv_maskDrawable">@drawable/material_drawer_rectangle_mask</item>
        <item name="biv_drawCircularShadow">false</item>
        <item name="biv_selectorOnPress">@color/material_drawer_primary</item>
        <item name="android:scaleType">centerInside</item>
    </style>
  7. Install MaterialDrawer via Gradle

    develop

    To use MaterialDrawer, add the dependency to your build.gradle file. For the latest Material 3 compatible version (v9.x), ensure you also include the required AndroidX and Material components.

    If you need Navigation Component support, add materialdrawer-nav. For icon font support (Android-Iconics), add materialdrawer-iconics.

    // Core dependency
    implementation("com.mikepenz:materialdrawer:${latestRelease}")
    
    // Required support libraries
    implementation "androidx.appcompat:appcompat:${versions.appcompat}"
    implementation "androidx.recyclerview:recyclerview:${versions.recyclerView}"
    implementation "androidx.annotation:annotation:${versions.annotation}"
    implementation "com.google.android.material:material:1.5.0-alpha05" // requires at least 1.5.0-x
    implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}"
    
    // Optional: NavController support
    implementation "com.mikepenz:materialdrawer-nav:${lastestMaterialDrawerRelease}"
    
    // Optional: Android-Iconics support
    implementation "com.mikepenz:materialdrawer-iconics:${lastestMaterialDrawerRelease}"
  8. Open the drawer in Espresso instrumental tests

    develop

    To open the MaterialDrawer during an Espresso instrumental test, you must use the DrawerActions.open() method. This requires the espresso-contrib library to be present in your project dependencies. You must target the drawer layout ID, which by default is R.id.material_drawer_layout.

    // 1. Ensure espresso-contrib is in your androidTest dependencies
    // androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
    
    // 2. Perform the open action using the default drawer layout ID
    onView(withId(R.id.material_drawer_layout)).perform(DrawerActions.open());
  9. Create a MaterialDrawer without a default selection

    develop

    To initialize a MaterialDrawer without any item being selected by default, set the selection position to -1 using setSelectionAtPosition(-1).

    //just set the selection to -1
    slider.setSelectionAtPosition(-1)
  10. Implement custom drawer items in MaterialDrawer

    develop

    To add unique functionality or UI to your MaterialDrawer, you can implement your own CustomDrawerItem. This allows you to go beyond the standard built-in items by defining your own layout and interaction logic.

    For implementation details and best practices, refer to the following resources:

    • Implementation Guides: Detailed StackOverflow discussions on implementing proper custom items can be found at this link and this link.
    • Reference Implementations: You can view existing custom drawer item implementations within the MaterialDrawer repository at app/src/main/java/com/mikepenz/materialdrawer/app/draweritems to use as a template.
    • Community Troubleshooting: For specific issues or edge cases regarding CustomDrawerItem, search the GitHub Issues tracker.
  11. Configure MaterialDrawer styles in your theme

    develop

    To apply the drawer's default styling, add materialDrawerStyle and materialDrawerHeaderStyle to your application theme.

    Note: If you are using v9.x with Material 3, your Activity must use a Material3 theme as its base.

    <style name="SampleApp.DayNight" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="materialDrawerStyle">@style/Widget.MaterialDrawerStyle</item>
        <item name="materialDrawerHeaderStyle">@style/Widget.MaterialDrawerHeaderStyle</item>
    </style>