3

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;
    }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
JCvanDamme
  • 621
  • 5
  • 20
  • 2
    maybe the text color is white ? – Shay Kin Apr 06 '21 at 19:06
  • you can check if the text is white by using `app:showAsAction="always"` – Shay Kin Apr 06 '21 at 19:16
  • by default text color is black I guess – Ankit Mishra Apr 06 '21 at 19:17
  • What is the theme used in the app? – Gabriele Mariotti Apr 06 '21 at 22:14
  • @GabrieleMariotti in the `` I set `android:theme="@style/Theme.Deck.NoActionBar"` and on the `Toolbar` itself I set `android:theme="?attr/actionBarTheme"`. It turns out that indeed, as @ShayKin suggested, the text color of the menu Items is white. The text color turns black if I remove the theme on ``, but this affects not only the text color of the drop-down items, but also of the menu itself. Any idea where I can set the text color in the drop-down menu? – JCvanDamme Apr 06 '21 at 22:39
  • @JCvanDamme What is Theme.Deck.NoActionBar? – Gabriele Mariotti Apr 06 '21 at 22:44
  • @GabrieleMariotti `Theme.Deck.NoActionBar` is a theme that inherits from `Theme.MaterialComponents.DayNight.DarkActionBar`. I could solve the problem as I posted in https://stackoverflow.com/a/66977475/4275167 . Many thanks – JCvanDamme Apr 06 '21 at 22:56
  • @JCvanDamme You can also use [app:popupTheme](https://stackoverflow.com/a/66977611/2016562). In this way you are changing only the popup in the Toolbar. – Gabriele Mariotti Apr 06 '21 at 23:13

2 Answers2

2

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

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>
JCvanDamme
  • 621
  • 5
  • 20