Android toolbar menu items remain empty

Viewed 596

My Android App has a Toolbar to which I want to add menu items. The Toolbar is displayed correctly, but when expanding the menu the menu items are void.

android toolbar menu item is void

The menu structure is defined in toolbar_menu.xml:

toolbar_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/about"
        android:title="About"
        app:showAsAction="never"/>
</menu>

The activity defines the toolbar in the layout file activity_layout.xml

activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:visibility="visible"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="Welcome"/>

    <!-- other widgets follow -->

</androidx.constraintlayout.widget.ConstraintLayout>

And the activity class where I set the Toolbar is MainActivity.java

MainActivity.java

// import dependencies

public class MainActivity extends AppCompatActivity {

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

        final Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        this.setSupportActionBar(toolbar);
    }




    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        this.getMenuInflater().inflate(R.menu.toolbar_menu, menu);
        return true;
    }
}
2 Answers

The issue is the text color in the popup menu.
Add the app:popupTheme attribute in your Toolbar.

    <androidx.appcompat.widget.Toolbar
        app:popupTheme="@style/AppTheme.PopupOverlay" 
        ../>

with:

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.DayNight.ActionBar" >
    <item name="android:textColor">@color/...</item>  
</style>

enter image description here

Thanks to the comment of @GabrieleMariotti below I realized that the issue and the solution are the same as https://stackoverflow.com/a/63279563/4275167.

I am using a custom theme Theme.Deck.NoActionBar that inherits from Theme.MaterialComponents.DayNight.DarkActionBar.

themes.xml

<style name="Theme.Deck" parent="Theme.MaterialComponents.DayNight.DarkActionBar">...</style>

<style name="Theme.Deck.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:itemTextAppearance">@style/menuItem</item> <!-- added to solve the problem -->
</style>

styles.xml

<!-- added to solve the problem -->
<style name="menuItem" parent="Widget.AppCompat.TextView.SpinnerItem">
    <item name="android:textColor">@color/black</item>
</style>
Related