Search code examples
javaandroidmenuandroid-toolbarmenuitem

Menu Item's Tittle Not Showing


Menu item's title is not showing inside fragment. I have two items in menu file, The first one is with icon and tag showAsAction=always to show the icon in toolbar. The second one is only with tittle.

I don't know what's going wrong here.

All actions with the menu items is working. E.g below.

enter image description here

menu_sale.xml

<?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:title="Scanner"
        android:id="@+id/miScanner"
        android:icon="@drawable/ic_baseline_qr_code_scanner"
        android:iconTint="@color/white"
        app:showAsAction="always"
        />

    <item
        android:title="Add Credit Record"
        android:id="@+id/miAddCreditRecord"
        />

</menu>

The item without showAsAction='always' not showing the title in my app.

My java code to inflate the menu.

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_sale, menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.miScanner:
            scanCode();
            break;
        case R.id.miAddCreditRecord:
            showBottomSheetDialog();
            break;
    }
    return super.onOptionsItemSelected(item);
}

My theme theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.AzmiUnaniStore" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
    
    <style name="NoActionBar" parent="Theme.AzmiUnaniStore">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="BottomSheetDialogTheme" parent="Theme.Design.BottomSheetDialog">
    </style>

    <style name="BottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
        
    </style>
    
</resources>

POSSIBILITIES

The tittle is showing perfectly but when the action menu colour is white and the background is also white that's is the I am not able to see the tittle.

Thanks


Solution

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

        <com.google.android.material.appbar.MaterialToolbar
            app:popupTheme="@style/AppTheme.PopupOverlay" 
            ../>
    

    with:

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary" >
        <!-- text color -->
        <item name="android:textColor">@color/...</item>  
    </style>
    

    enter image description here