Android navigation drawer like Gmail Example

 Android navigation drawer like Gmail Example


To create an Android navigation drawer like the one used in the Gmail app, you will need to use the DrawerLayout widget provided by the Android support library.


Here's an example of how you can use the DrawerLayout widget in your layout XML file:

Image of Android navigation drawer like Gmail Example
 Image of Android navigation drawer like Gmail Example


In this example, the main content is a LinearLayout and the navigation drawer is a NavigationView. The NavigationView is given a menu resource file with the app:menu attribute, which specifies the items to be displayed in the navigation drawer. The app: header layout attribute specifies a layout file to be used as the header of the navigation drawer.


<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/drawer_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <!-- Your main content goes here -->

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical">


        <!-- Your main content goes here -->


    </LinearLayout>


    <!-- The navigation drawer -->

    <com.google.android.material.navigation.NavigationView

        android:id="@+id/nav_view"

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        app:headerLayout="@layout/nav_header"

        app:menu="@menu/nav_menu"/>


</androidx.drawerlayout.widget.DrawerLayout>


 

To open and close the navigation drawer, you can use the openDrawer() and closeDrawer() methods of the DrawerLayout widget in your Java code. You can also use the addDrawerListener() method to attach a listener that will be notified when the drawer is opened or closed.


For more information and a complete example, you can refer to the documentation for the DrawerLayout widget: https://developer.android.com/reference/android/support/v4/widget/DrawerLayout

Post a Comment

Previous Post Next Post