Search code examples
androidnavigation-drawermaterialdrawer

Visible ActionBar with opened NavDrawer


Is it possible to keep the actionbar visible using the mikepenz's material drawer library?

Visible ActionBar with opened NavDrawer


Solution

  • Yes this is possible. Just see the sample application. This contains the CustomContainerActivity which showcases how you can have a Drawer which is below the Toolbar

    public class CustomContainerActivity extends AppCompatActivity {
    
        //save our header or result
        private Drawer result = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sample_custom_container_dark_toolbar);
    
            // Handle Toolbar
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(R.string.drawer_item_custom_container_drawer);
    
            //Create the drawer
            result = new DrawerBuilder(this)
                    //this layout have to contain child layouts
                    .withRootView(R.id.drawer_container)
                    .withToolbar(toolbar)
                    .withActionBarDrawerToggleAnimated(true)
                    .addDrawerItems(
                            new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
                            new PrimaryDrawerItem().withName(R.string.drawer_item_free_play).withIcon(FontAwesome.Icon.faw_gamepad),
                            new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye),
                            new SectionDrawerItem().withName(R.string.drawer_item_section_header),
                            new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog),
                            new SecondaryDrawerItem().withName(R.string.drawer_item_help).withIcon(FontAwesome.Icon.faw_question).withEnabled(false),
                            new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
                            new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_bullhorn)
                    )
                    .withSavedInstance(savedInstanceState)
                    .build();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            //add the values which need to be saved from the drawer to the bundle
            outState = result.saveInstanceState(outState);
            super.onSaveInstanceState(outState);
        }
    
        @Override
        public void onBackPressed() {
            //handle the back press :D close the drawer first and if the drawer is closed close the activity
            if (result != null && result.isDrawerOpen()) {
                result.closeDrawer();
            } else {
                super.onBackPressed();
            }
        }
    }
    

    Also note the different xml layout which is used in this Activity.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:elevation="4dp" />
    
        <!-- the layout which will contain (host) the drawerLayout -->
        <FrameLayout
            android:layout_below="@id/toolbar"
            android:id="@+id/drawer_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <!-- the layout which will be the content of the activity (which will be hosted inside the drawer (NOT the list of the drawer)) -->
            <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <TextView
                    android:id="@+id/txtLabel"
                    android:layout_gravity="center"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test"
                    android:textSize="16sp" />
            </FrameLayout>
        </FrameLayout>
    </RelativeLayout>
    

    This will produce the following:

    CustomContainerActivity MaterialDrawer