Tyler M.
Tyler M.

Reputation: 125

Android Toolbar Menu Not Displaying on All Activities

I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.

Here is the toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

    app:popupTheme="@style/Theme.AppCompat.Light">

</androidx.appcompat.widget.Toolbar>

Here is the settings_menu.xml that should show the 3-dot menu.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/item2"
        android:title="Item 2"
        app:showAsAction="never" />

    <item android:id="@+id/item3"
        android:title="Item 3"
        app:showAsAction="never">

        <menu>

            <item android:id="@+id/subitem1"
                android:title="Sub Item 1"/>

            <item android:id="@+id/subitem2"
                android:title="Sub Item 2"/>

        </menu>

    </item>

</menu>

Here is the onCreate() method of HomeActivity, where the menu DOES appear.

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

setContentView(R.layout.activity_home);

toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);

buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);

// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();

And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);

toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);

buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        createClient();
    }
});

// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}

Upvotes: 0

Views: 129

Answers (1)

Mohammed Alaa
Mohammed Alaa

Reputation: 3320

you have to override the onCreateOptionsMenu in every activity you need menu to be shown within

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.settings_menu, menu);
    // return true so that the menu pop up is opened
    return true; 
}

check this thread for full details

Upvotes: 1

Related Questions