create menu android code example

Example 1: android toolbar with menus

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

Example 2: menu android

// main xml
 <androidx.appcompat.widget.Toolbar
        android:id="@+id/activity_main_toolbar"
        android:minHeight="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:titleTextColor="@android:color/white"
        android:background="?attr/colorPrimary"/>
 // menu menu/..menu.xml 
 
 <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_activity_main_search"
        android:icon="@drawable/ic_search_white_24dp"
        app:showAsAction="ifRoom"
        android:title="Rechercher">
    </item>
    <item
        android:id="@+id/menu_activity_main_params"
        android:icon="@drawable/ic_more_vert_white_24dp"
        app:showAsAction="ifRoom"
        android:title="Paramètres">
    </item>
</menu>

Tags:

Java Example