How to create a custom Navigation Drawer Menu in Android Studio.

1st, We have to create a string value and place it in the string.xml layout.

<string name="custom_app_name">Write App Name</string>

    <!-- Navigation Menu Items -->
    <string name="more_apps">Visit More Apps</string>
    <string name="rate_this_app">Rate this Application</string>
    <string name="communication">Communication</string>
    <string name="share">Share</string>
    <string name="disclaimer">Disclaimer</string>
    <string name="privacy_policy">Privacy Policy</string>
    <string name="terms_conditions"><![CDATA[Terms & Conditions]]></string>
    <string name="categories_title">Categories Title</string>

    <!-- Drawer Layout Items -->
    <string name="drawer_open">Drawer Open</string>
    <string name="drawer_close">Drawer Close</string>

    <!-- Navigation Menu Links -->
    <string name="text_plain">text/plain</string>
    <string name="AppDescription"><b>आवास योजना की नई सूची 2022</b> : के तहत जो भी लिस्ट निकलता है उसका सभी विवरण यंहा तत्काल अपलोड कर दिया जाता है , <b>Download</b> करें इस Application को और अपने सुविधा अनुसार उपयोग करें ! </string>
    <string name="AppLink"><![CDATA[https://play.google.com/store/apps/details?id=com.npc2cstudio.pmawasyojna&hl=en]]></string>
    <string name="sub_here">Your Subject Here</string>
    <string name="shareUsing">Share using</string>
    <string name="packageDetails">market://details?id=</string>
    <string name="packageDetail">http://play.google.com/store/apps/details?id=</string>
    <string name="privacy_policy_link">https://androidapptemplate.com/index.php/2023/08/15/privacy-policy-of-multi-webview-app/</string>
    <string name="terms_condition_link">https://androidapptemplate.com/index.php/2023/08/15/terms-conditions-of-multi-webview-app/</string>
    <string name="developerApps">https://play.google.com/store/apps/developer?id=pub:NPC2C+Studio</string>
    <string name="DeveloperApp"><![CDATA[market://search?q=pub:NPC2C+Studio&c=apps]]></string>
    <string name="about_disclaimer">Content presented in this app is collected from the Internet. We do not have any right of any kind on this materials and we invalidate all types of realities about this material.\n\nIf you have any personal ownership rights on any of the content shown in the application than you can inform us by sending an E-mail to given below with proof of ownership/authority.\n\nWe will check the validity of your claim doing so will remove content immediately.</string>
    <string name="about_disclaimer_short">We just provide links for any issue we are not responsible.\nIf any issue please contact to owner of website.</string>
    <string name="email_id">Email Us :</string>
    <string name="emailId">npc2cstudio@gmail.com</string>
    
    <string name="youtube">YouTube</string>

2nd, We have to create a color code for the Android application.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#024265</color>
    <color name="purple_500">#024265</color>
    <color name="purple_700">#024265</color>
    <color name="teal_200">#FDDBE9</color>
    <color name="teal_700">#FDDBE9</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
    <color name="green">#019E07</color>
    <color name="orange">#d2601a</color>
    <color name="blue">#033BE3</color>
</resources>

3rd, We have to create a navigation menu icon and drawable folder code given below:

  • dialog_background
  • ic_nav_menu_info
  • ic_nav_menu_more_apps
  • ic_nav_menu_privacy_tip
  • ic_nav_menu_security
  • ic_nav_menu_star_half
  • ic_nav_share
  • navigation_menu_background

3rd(A). dialog_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="@color/white" />
    <corners android:radius="12dp" />
</shape>

3rd(B). ic_nav_menu_info

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/purple_200"
        android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z" />
</vector>

3rd(C). ic_nav_menu_more_apps

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/green"
        android:pathData="M16,20m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
    <path
        android:fillColor="@color/green"
        android:pathData="M6,20m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" />
    <path
        android:fillColor="@color/blue"
        android:pathData="M22,7V6.48C22,4.56 20.52,3 18.65,3c-1.66,0 -2.54,1.27 -3.18,2.03l-8.8,10.32C6.12,16 6.58,17 7.43,17L15,17c1.1,0 2,-0.9 2,-2V6.27C17.58,5.59 17.97,5 18.65,5C19.42,5 20,5.66 20,6.48V7H22z" />
    <path
        android:fillColor="@color/orange"
        android:pathData="M14.3,4.1C13.03,3.4 11.56,3 10,3C8.03,3 6.21,3.64 4.72,4.72l4.89,4.89L14.3,4.1z" />
</vector>

3rd(D). .ic_nav_menu_privacy_tip

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/purple_200"
        android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12c5.16,-1.26 9,-6.45 9,-12V5L12,1L12,1zM11,7h2v2h-2V7zM11,11h2v6h-2V11z" />
</vector>

3rd(E). .ic_nav_menu_security

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/purple_200"
        android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12 5.16,-1.26 9,-6.45 9,-12L21,5l-9,-4zM12,11.99h7c-0.53,4.12 -3.28,7.79 -7,8.94L12,12L5,12L5,6.3l7,-3.11v8.8z" />
</vector>

3rd(F). .ic_nav_menu_star_half

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/purple_200"
        android:pathData="M22,9.24l-7.19,-0.62L12,2L9.19,8.63L2,9.24l5.46,4.73L5.82,21L12,17.27L18.18,21l-1.63,-7.03L22,9.24zM12,15.4V6.1l1.71,4.04l4.38,0.38l-3.32,2.88l1,4.28L12,15.4z" />
</vector>

3rd(G). .ic_nav_share

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="@color/purple_200"
        android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z" />
</vector>

3rd(H). .navigation_menu_background

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:bottomLeftRadius="0dp"
        android:bottomRightRadius="500dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp" />
    <solid android:color="@color/teal_200" />

</shape>

4th, We have to create a custom nav_header layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:background="@color/purple_200"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/imgNavHeader"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:contentDescription="@string/app_name"
        android:elevation="2dp"
        android:src="@drawable/logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1" />


</androidx.constraintlayout.widget.ConstraintLayout>

5th, we have to create a custom dialog_disclaimer layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:background="@drawable/dialog_background"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="60dp"
            android:text="@string/disclaimer"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/purple_200"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="12dp"
            android:justificationMode="inter_word"
            android:text="@string/about_disclaimer"
            android:textColor="@color/black"
            android:textSize="14sp"
            tools:ignore="UnusedAttribute" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.4dp"
            android:layout_marginTop="8dp"
            android:background="@color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="12dp"
            android:justificationMode="inter_word"
            android:text="@string/about_disclaimer_short"
            android:textColor="@color/purple_200"
            android:textSize="15sp"
            android:textStyle="bold|italic"
            tools:ignore="UnusedAttribute" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0.4dp"
            android:layout_marginTop="8dp"
            android:background="@color/black" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="6dp"
            android:text="@string/email_id"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/purple_200"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="4dp"
            android:layout_marginRight="12dp"
            android:layout_marginBottom="12dp"
            android:text="@string/emailId"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/purple_200" />


    </LinearLayout>

    <ImageView
        android:id="@+id/dialog_image"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/app_name"
        android:padding="10dp"
        app:srcCompat="@drawable/logo" />

</RelativeLayout>

6th, we have to create a custom toolbar layout for navigation drawer in activity_main.xml layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/teal_200"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/purple_200">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/constraintLayout"
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:layout_margin="8dp"
                android:background="@color/teal_200"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0">

                <com.google.android.material.appbar.AppBarLayout
                    android:id="@+id/appBarLayout"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_margin="2dp"
                    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/toolBar"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                        <androidx.constraintlayout.widget.ConstraintLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                            <TextView
                                android:id="@+id/textView"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:fontFamily="@font/alatsi"
                                android:shadowColor="@color/black"
                                android:shadowDx="1.5"
                                android:shadowDy="1.3"
                                android:shadowRadius="1.6"
                                android:text="@string/app_name"
                                android:textColor="@color/teal_200"
                                android:textSize="22sp"
                                android:textStyle="bold"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintEnd_toEndOf="parent"
                                app:layout_constraintHorizontal_bias="0"
                                app:layout_constraintStart_toStartOf="parent"
                                app:layout_constraintTop_toTopOf="parent" />

                        </androidx.constraintlayout.widget.ConstraintLayout>
                    </androidx.appcompat.widget.Toolbar>
                </com.google.android.material.appbar.AppBarLayout>

            </androidx.constraintlayout.widget.ConstraintLayout>

            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/constraintLayout" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:background="@color/teal_200"
                app:layout_constraintBottom_toTopOf="@+id/banner_ad_container"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view" />

            <LinearLayout
                android:id="@+id/banner_ad_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationMenu"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/navigation_menu_background"
        android:scrollbars="vertical"
        app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

6th(A), we have to create a custom navigation drawer(navigation_menu) menu in menu folder.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">

        <item
            android:id="@+id/nav_more_apps"
            android:icon="@drawable/ic_nav_menu_more_apps"
            android:title="@string/more_apps" />

        <item
            android:id="@+id/nav_rate_this_app"
            android:icon="@drawable/ic_nav_menu_star_half"
            android:title="@string/rate_this_app" />

    </group>
    <item android:title="@string/communication">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_nav_share"
                android:title="@string/share" />
            <item
                android:id="@+id/nav_disclaimer"
                android:icon="@drawable/ic_nav_menu_info"
                android:title="@string/disclaimer" />
            <item
                android:id="@+id/nav_privacy_policy"
                android:icon="@drawable/ic_nav_menu_security"
                android:title="@string/privacy_policy" />
            <item
                android:id="@+id/nav_terms_conditions"
                android:icon="@drawable/ic_nav_menu_privacy_tip"
                android:title="@string/terms_conditions" />
        </menu>
    </item>

</menu>

6th(B), we have to create a custom actionbar toolbar(first_action_bar) menu in menu folder.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/youtube"
        android:icon="@drawable/video_logo"
        android:title="@string/youtube"
        app:showAsAction="ifRoom" />

</menu>

7th, We have to do all the above XML layout code and now we have to create a JAVA code for the backend working system given below:

  • Create some variable codes.
  • Create a ToolBar.
  • Create a Drawer Layout.
  • Create a Navigation Drawer Menu.
  • Create an Options Menu.
  • Create an Option Items Selected Menu.
  • Create User Menu Selector.

7th(A): Create some Variable Codes.

ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;
    View view;

7th(B): Create a ToolBar.

Toolbar toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setTitle(getResources().getString(R.string.app_name));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

7th(C): Create a Drawer Layout.

drawerLayout = findViewById(R.id.drawerLayout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

7th(D): Create a Navigation Drawer Menu.

NavigationView navigationView = findViewById(R.id.navigationMenu);
        view = navigationView.inflateHeaderView(R.layout.nav_header);
        navigationView.setItemIconTintList(null);

        navigationView.setNavigationItemSelectedListener(item -> {
            UserMenuSelector(item);
            return false;
        });

7th(E): Create an Options Menu.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.first_action_bar, menu);
        return true;
    }

7th(F): Create an Options Items Selected Menu.

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.youtube) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/@click2code"));
            startActivity(intent);
            return true;
        }
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

7th(G): Create User Menu Selector.

    private void UserMenuSelector(MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == R.id.nav_more_apps) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.DeveloperApp)));
                startActivity(intent);
            } catch (Exception ex) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.developerApps)));
                startActivity(intent);
            }
        } else if (itemId == R.id.nav_rate_this_app) {
            try {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("market://details?id=" + getPackageName())));

            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://play.google.com/store/apps/details?id=" + getPackageName())));
            }
        } else if (itemId == R.id.nav_share) {
            Intent myIntent = new Intent(Intent.ACTION_SEND);
            myIntent.setType("text/plain");
            String shareBody = "Multi WebView App में जो लिस्ट और detail जारी हुआ है उसका सभी विवरण इस App में दिया हुआ है। \n" + "https://play.google.com/store/apps/details?id=com.codeapp.webviewapp&hl=en";
            String shareSub = "Your Subject Here";
            myIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub);
            myIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(myIntent, "Share using"));
        } else if (itemId == R.id.nav_disclaimer) {
            Dialog dialog = new Dialog(this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialog_disclaimer);
            Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();
        } else if (itemId == R.id.nav_privacy_policy) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.privacy_policy_link)));
            startActivity(browserIntent);
        } else if (itemId == R.id.nav_terms_conditions) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.terms_condition_link)));
            startActivity(browserIntent);
        }

        drawerLayout.closeDrawer(GravityCompat.START);
    }
Scroll to Top