free web tracker
Course Content
Java Programming Basics for Android
Learn the basics of Java programming for Android app development using Android Studio. This guide covers key concepts like variables, loops, and classes to help you start building your first Android apps with confidence. Perfect for beginners!
0/10
User Interaction and App Logic
Learn how to make your app respond to users! This section covers handling clicks, getting input, showing messages, switching screens, and saving simple data. A perfect start to build interactive Android apps with real logic.
0/10
Advanced Layouts and Components
Learn to build modern Android UIs using advanced layouts like RecyclerView, CardView, TabLayout, and more. This section helps beginners create beautiful, interactive, and user-friendly app interfaces step by step.
0/10
Media and Resources
Learn how to manage media and resources in Android Studio. This section covers adding audio, images, video, using drawables, custom fonts, and handling runtime permissions—essential for building rich, engaging Android applications.
0/4
Mastering Java Android Development – Beginner

Menus allow users to navigate or trigger actions from the app interface. In Android, you can use OptionsMenu to show a list of items when the user taps the overflow icon (three dots).

Step 1: Create a Menu XML File

    1. Inside the res folder, go to res > menu.
    2. Right-click the menu folder → New → Menu resource file.
    3. Name the file main_menu.xml and paste the following code:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_settings"
        android:title="Settings"
        android:icon="@android:drawable/ic_menu_preferences"
        android:showAsAction="ifRoom"/>
    <item
        android:id="@+id/menu_about"
        android:title="About"
        android:icon="@android:drawable/ic_menu_info_details"
        android:showAsAction="never"/>
</menu>

Step 2: Inflate the Menu in Your Java Activity

Inside your Java activity (e.g., MainActivity.java), override onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

Step 3: Handle Menu Item Clicks

Override onOptionsItemSelected() in the same activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.menu_settings) {
        Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id == R.id.menu_about) {
        Toast.makeText(this, "About clicked", Toast.LENGTH_SHORT).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Final Result

When you run your app, you’ll see a menu button (usually in the top-right corner). Clicking it will display the defined menu items (e.g., “Settings” and “About”). Clicking those will trigger the toast messages.

Tips

    • Use icons to improve UX.
    • You can conditionally hide/show items using menu.findItem(R.id.id).setVisible(true/false).
    • For context menus or popup menus, consider using PopupMenu or ContextMenu.