Menu Navigation Layout in Android Studio

Menu Navigation Layout design in Android will help users navigate between the activities or fragments. You will see the Code example for the navigation layout view in Java. Here, one main activity will be there to navigate the activities.

Navigation handing in on Click Event called NavClick() function will navigate based on Activity Id setNavigationItemSelectedListener will take into the selected activity in frame layout.

Java Code

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        layDL = findViewById(R.id.layDL);
        vNV = findViewById(R.id.vNV);
        toolbar = findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, layDL, toolbar, R.string.open_drawer, R.string.close_drawer);

        layDL.addDrawerListener(toggle);
        toggle.syncState();

        if (savedInstanceState == null) {
            vNV.setCheckedItem(R.id.General);
        }
        NavClick();
    }
    private void NavClick() {
        vNV.setNavigationItemSelectedListener(item -> {
            Fragment frag = null;
            if (item.getItemId() == R.id.Home) {
               // Toast.makeText(SecondActivity.this,"Home",Toast.LENGTH_SHORT).show();
                Intent objIntent = new Intent(SecondActivity.this, HomeActivity.class);
                startActivity(objIntent);
            }
            if (item.getItemId() == R.id.About) {
               // Toast.makeText(ProgrammingParam.this,"About",Toast.LENGTH_SHORT).show();
                Intent objIntent = new Intent(SecondActivity.this, AboutActivity.class);
                startActivity(objIntent);
            } if (item.getItemId() == R.id.Contact) {
                //Toast.makeText(SecondActivity.this,"Contact",Toast.LENGTH_SHORT).show();
                Intent objIntent = new Intent(SecondActivity.this, ContactActivity.class);
                startActivity(objIntent);

            }
            layDL.closeDrawer(GravityCompat.START);
            return true;
        });
    

XML

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layDL"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    tools:context=".SecondActivity">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:elevation="0dp">

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#2F8C46"
                app:title="Meter Programming"
                app:titleTextColor="@color/white"
                android:id="@+id/toolbar" />
        </com.google.android.material.appbar.AppBarLayout>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/layFL" />
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Checking Navigation Drawer Item"
        android:textColor="#2F8C46"
        android:textStyle="bold|italic"
        android:textSize="20sp" />
    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/vNV"
        app:menu="@menu/nav_item2"
        app:headerLayout="@layout/nav_header"
        android:layout_gravity="start"
        android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>

Output

Navigation Menu layout in Android


Similar Articles