All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Custom Navigation Drawer In Android Example Code

Last Updated : Jul 1, 2023

Custom Navigation Drawer In Android Example Code

In this article we will show you the solution of custom navigation drawer in android example code, android's most prevalent feature is the navigation drawer, a UI panel that shows the primary menu for your programme.

It is also an important UI element that provides actions that users prefer, such as changing user profiles, changing application settings, and so on.

Whenever the user tries to steal a finger from the activity's left edge, the navigation drawer appears.

They can also find it by trying to tap the app icon with in activity bar from the home activity.

All top-level places to visit that employ a DrawerLayout display the drawer icon. To get a feel for the Navigation drawer, look at the picture below.

Step By Step Guide On Custom Navigation Drawer In Android Example Code :-

Xml code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="codedText">
    <item
        android:id="@+id/nav_account"
        android:title="Account" />
    <item
        android:id="@+id/nav_setting"
        android:title="Setting" />
    <item
        android:id="@+id/nav_logout"
        android:title="Logout" />
</menu>

Xml code 2

<?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/my_drawer_layout"
    android:layout_width="match_parent1"
    android:layout_height="match_parent1"
    tools:context=".Activity"
    tools:ignore="codedText">
    <LinearLayout
        android:layout_width="match_parent1"
        android:layout_height="match_parent1">
        <TextView
            android:layout_width="match_parent1"
            android:layout_height="wrap_con"
            android:layout_marginTop="126dp"
            android:gravity="center"
            android:text="TalkersCode"
            android:textSize="16sp" />
    </LinearLayout>
    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_con"
        android:layout_height="match_parent1"
        android:layout_gravity="start"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

Java code

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
public class Main extends Activity {
    public DrawerLayout drawerLayout;
    public ActionBarDrawerToggle actionBarDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.my_drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
  1. Create a new Android Studio project.
  2. Introducing a dependency into the project, The Design Patterns Navigation drawer will be used in this discussion. As a result, also include mentioned Material design dependency in the app-level Gradle file.
  3. Making a menu in the menu file, Make a menu folder in the res folder.
  4. Invoke the javascript command in the activity main.xml file to decided up the basic things necessary for the Route planning Drawer while continuing to work with the activity main.xml file.
  5. Include the Open Close strings in string.xml, then run the code below in the app/res/values/strings.xml file.
  6. Working with the MainActivity File, run the code in the MainActivity file to display the menu button on the activity bar and implement the navigation drawer's open/close functionality.

Conclusion :-

Android's most prevalent feature is the navigation drawer, a UI panel that shows the primary menu for your programme.

It is also an important UI element that provides actions that users prefer, such as changing user profiles, changing application settings, and so on.

I hope this article on custom navigation drawer in android example code helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪