In this blog, we are going to create a navigation drawer menu for the calendar/any app. And we have to follow step by step method then we can create a navigation drawer menu successfully.
Step-01
First, we have to create some string.xml code which is help to navigation system in an easy way.
<!-- Drawer Layout Items --> <string name="drawer_open">Drawer Open</string> <string name="drawer_close">Drawer Close</string> <string name="custom_app_name">ठाकुर प्रसाद कैलेंडर - 2023</string> <!-- Navigation Menu Items --> <string name="more_apps"><b>अधिक ऐप्स पर जाएं !</b></string> <string name="rate_this_app"><b>इस App को रेट करे !</b></string> <string name="communication">Communication</string> <string name="share">इस App को शेयर कीजिए !</string> <string name="disclaimer">Disclaimer</string> <string name="privacy_policy">Privacy Policy</string> <string name="terms_conditions">Terms & Conditions</string> <string name="rate_us">🌻 बेहतर फीचर्स के लिए इस app को रेट करें 🌻</string> <string name="rate_us_5_star">⭐ Rate Us 5 Star ⭐</string> <!-- Navigation Menu Links --> <string name="text_plain">text/plain</string> <string name="AppDescription"><b>ठाकुर प्रसाद कैलेंडर 2023</b> : इस कैलेंडर ऐप्प में आपको इस साल का पूरा महीना देखने को मिलेगा साथ ही साथ पूरा साल का :- दिशा सूल, सूर्य ग्रहण, चंद्र ग्रहण, गृह गोचर, पूजा पाठ, पंचक और अमावस्या का विवरण और शुभ मुहूर्त, त्यौहार, और राशिफल देखने को मिलेगा। इसलिए जल्दी से इस कैलेंडर ऐप्प को Download कीजिए। धन्यवाद ! </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="privacyLink">https://click2code.com/privacy-policy-of-thakur-prasad-calendar-2023/</string> <string name="termsLink">https://click2code.com/terms-conditions-of-thakur-prasad-calendar-2023/</string> <string name="developerApps">https://play.google.com/store/apps/developer?id=pub:SLPPSAJ+Studio</string> <string name="DeveloperApp"><![CDATA[market://search?q=pub:SLPPSAJ+Studio&c=apps]]></string>
Step-02
Now, we have created a menu items icon which’s shown in the navigation drawer menu. So all 5 item icon code is given below.
- ic_nav_menu_more_apps
- ic_nav_menu_privacy_tip
- ic_nav_menu_security
- ic_nav_menu_star_half
- ic_nav_share
ic_nav_menu_more_apps code is given below:
<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="M16,20m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" /> <path android:fillColor="@color/purple_200" android:pathData="M6,20m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" /> <path android:fillColor="@color/purple_200" 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/purple_200" 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>
ic_nav_menu_privacy_tip code is given below:
<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>
ic_nav_menu_security code is given below:
<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>
ic_nav_menu_star_half code is given below:
<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>
ic_nav_share code is given below:
<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>
Step-03
Now, we have to create a navigation menu in the menu folder so first you have to create a menu folder in your project if you do not know how to create then you have to watch below YouTube video. So, you have to give the name nav_menu.xml
<?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_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>
Step-04
Now, we have to create a nav_header.xml layout which is shown in the navigation header in the navigation drawer menu.
<?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_500" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/headerImageView" android:layout_width="80dp" android:layout_height="80dp" android:contentDescription="@string/app_name" android:src="@drawable/icon" 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" app:layout_constraintVertical_bias="0.5" /> <TextView android:id="@+id/headerTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:shadowColor="@color/blue" android:shadowDx="1.5" android:shadowDy="1.3" android:shadowRadius="1.6" android:text="@string/custom_app_name" android:textColor="@color/white" 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_toBottomOf="@+id/headerImageView" app:layout_constraintVertical_bias="0" /> </androidx.constraintlayout.widget.ConstraintLayout>
Step-05
Now, we have to create a NavigationView under DrawerLayout in layout_home.xml.
<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:scrollbars="vertical" app:menu="@menu/nav_menu" />
Step-06
Now, we have to create a Java code in HomeActivity.java.
First, we have to create a ToolBar Java code.
Toolbar toolbar = findViewById(R.id.toolBar); setSupportActionBar(toolbar); Objects.requireNonNull(getSupportActionBar()).setTitle(null); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Now, we have to create a navigation menu in Java code in the onCreate method.
drawerLayout = findViewById(R.id.drawerLayout); actionBarDrawerToggle = new ActionBarDrawerToggle(HomeActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_close); actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white)); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.syncState(); NavigationView navigationView = findViewById(R.id.navigationMenu); view = navigationView.inflateHeaderView(R.layout.nav_header); navigationView.setItemIconTintList(null); navigationView.setNavigationItemSelectedListener(item -> { UserMenuSelector(item); return false; });
Now, we have to create the onOptionItemSelected method in AppCompactActivity.
@Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); }
Now, we have to create a custom UserMenuSelector in AppCompactActivity.
@SuppressLint("NonConstantResourceId") private void UserMenuSelector(MenuItem item) { switch (item.getItemId()) { case 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); } break; case R.id.nav_share: Intent myIntent = new Intent(Intent.ACTION_SEND); myIntent.setType(getString(R.string.text_plain)); String shareBody = getString(R.string.AppDescription) + getString(R.string.AppLink); String shareSub = getString(R.string.sub_here); myIntent.putExtra(Intent.EXTRA_SUBJECT, shareSub); myIntent.putExtra(Intent.EXTRA_TEXT, shareBody); startActivity(Intent.createChooser(myIntent, getString(R.string.shareUsing))); break; case R.id.nav_rate_this_app: try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.packageDetails) + getPackageName()))); } catch (ActivityNotFoundException e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.packageDetail) + getPackageName()))); } break; case R.id.nav_privacy_policy: Intent privacyPolicy = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.privacyLink))); startActivity(privacyPolicy); break; case R.id.nav_terms_conditions: Intent termsConditions = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.termsLink))); startActivity(termsConditions); break; } drawerLayout.closeDrawer(GravityCompat.START); }
