Create a Navigation Drawer Menu in Android Studio in Kotlin.

If you want to create a navigation drawer menu separately, we first have to implement a color code in the color.xml file for this project.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#242424</color>
<color name="purple_500">#242424</color>
<color name="purple_700">#242424</color>
<color name="teal_200">#242424</color>
<color name="teal_700">#242424</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>

<color name="green">#019E07</color>
<color name="orange">#FF511A</color>
<color name="blue">#033BE3</color>
</resources>

Now, we will create a Navigation Drawer Menu for an Activity in Android Studio with Kotlin Language. We will follow the steps step by step.

<!-- 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>
<!-- 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>Multi WebView Template</b> : के तहत जो भी लिस्ट निकलता है उसका सभी विवरण यंहा तत्काल अपलोड कर दिया जाता है , <b>Download</b> करें इस Application को और अपने सुविधा अनुसार उपयोग करें ! </string>
<string name="AppLink"><![CDATA[https://play.google.com/store/apps/details?id=com.nilkanthpawan.nilkanthpawan&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://nilkanthpawanapps.blogspot.com/2024/08/privacy-policy.html</string>
<string name="termsLink">https://nilkanthpawanapps.blogspot.com/2024/08/terms-conditions.html</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>
<string name="refresh">Refresh</string>

Now, we have to insert the navigation menu icon in the drawer folder. so we have to download the icon press below button.

Download the navigation menu icon file.

Now, create a menu folder and then create a navigation_menu.xml file.

<?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>
<?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:fitsSystemWindows="true"
tools:context=".MainActivity">

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

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">

<FrameLayout
android:id="@+id/fragmentContainerLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="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:scrollbars="vertical"
app:menu="@menu/navigation_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

First, we paste the variable code below code.

private var actionBarDrawerToggle: ActionBarDrawerToggle? = null
private var view: View? = null
private var drawerLayout: DrawerLayout? = null

Second, we implement the Navigation Drawer Menu code under the onCreate method given below code.

val toolbar = findViewById<Toolbar>(R.id.toolBar)
setSupportActionBar(toolbar)
Objects.requireNonNull(supportActionBar)?.title = null
supportActionBar!!.setDisplayHomeAsUpEnabled(true)

drawerLayout = findViewById(R.id.drawerLayout)
actionBarDrawerToggle = ActionBarDrawerToggle(
this@MainActivity,
drawerLayout,
R.string.drawer_open,
R.string.drawer_close
)
actionBarDrawerToggle!!.drawerArrowDrawable.color = resources.getColor(R.color.white)
drawerLayout?.run { addDrawerListener(actionBarDrawerToggle!!) }
actionBarDrawerToggle!!.syncState()

val navigationView = findViewById<NavigationView>(R.id.navigationMenu)
view = navigationView.inflateHeaderView(R.layout.navigation_header)
navigationView.itemIconTintList = null

navigationView.setNavigationItemSelectedListener { item: MenuItem ->
UserMenuSelector(item)
false
}

Now, Create the onOptionsItemSelected method.

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (actionBarDrawerToggle!!.onOptionsItemSelected(item)) return true
return super.onOptionsItemSelected(item)
}

Now, Create the UserMenuSelector method.

private fun UserMenuSelector(item: MenuItem) {
val itemID = item.itemId

if (itemID == R.id.nav_more_apps) {
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.DeveloperApp)))
startActivity(intent)
} catch (ex: Exception) {
val intent =
Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.developerApps)))
startActivity(intent)
}
} else if (itemID == R.id.nav_rate_this_app) {
try {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=$packageName")
)
)
} catch (e: ActivityNotFoundException) {
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=$packageName")
)
)
}
} else if (itemID == R.id.nav_share) {
val myIntent = Intent(Intent.ACTION_SEND)
myIntent.setType(getString(R.string.text_plain))
val shareBody = getString(R.string.AppDescription) + getString(R.string.AppLink)
val 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)))
} else if (itemID == R.id.nav_disclaimer) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerLayout, DisclaimerFragment(), null)
.addToBackStack(null).commit()
} else if (itemID == R.id.nav_privacy_policy) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerLayout, PrivacyPolicy(), null).addToBackStack(null)
.commit()
} else if (itemID == R.id.nav_terms_conditions) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainerLayout, TermsConditions(), null).addToBackStack(null)
.commit()
}
drawerLayout!!.closeDrawer(GravityCompat.START)
}

Scroll to Top