Upgrade to Pro

How to Add a slide menu (drawer menu / sidebar menu) in your Android Studio app and link the menu items to your own custom website links.

1. Add DrawerLayout to Your Activity Layout

Open your activity_main.xml (or your main activity layout), then wrap your main layout in a DrawerLayout and add a NavigationView for the menu.

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main content -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Navigation Drawer -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/nav_header" />

</androidx.drawerlayout.widget.DrawerLayout>

2. Create the Drawer Menu Items

Inside res/menu/ create a file named drawer_menu.xml.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home"
        android:title="Home"
        android:icon="@android:drawable/ic_menu_view" />

    <item
        android:id="@+id/nav_blog"
        android:title="Blog"
        android:icon="@android:drawable/ic_menu_info_details" />

    <item
        android:id="@+id/nav_contact"
        android:title="Contact Us"
        android:icon="@android:drawable/ic_menu_call" />
</menu>

3. Add a Toolbar with Menu Icon

In your activity XML, add:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

4. Handle Drawer + Links in Activity

In MainActivity.java (or Kotlin if you use it):

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    DrawerLayout drawerLayout;
    NavigationView navigationView;
    ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);

        toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        navigationView.setNavigationItemSelectedListener(item -> {
            int id = item.getItemId();

            if (id == R.id.nav_home) {
                openLink("https://yourwebsite.com/home");
            } else if (id == R.id.nav_blog) {
                openLink("https://yourwebsite.com/blog");
            } else if (id == R.id.nav_contact) {
                openLink("https://yourwebsite.com/contact");
            }

            drawerLayout.closeDrawers();
            return true;
        });
    }

    private void openLink(String url) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(browserIntent);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull android.view.MenuItem item) {
        if (toggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

5. Strings (optional)

In res/values/strings.xml add:

<string name="open">Open</string>
<string name="close">Close</string>

 Now you have:

  • A slide drawer menu icon (hamburger icon).

  • Menu items linked to custom website URLs.

  • When clicked, the link opens in a browser (you can also make it load in a WebView if you prefer).

Flowisetech For easy access