4

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
mufazmi
  • 1,103
  • 4
  • 18
  • 37
  • @color/white – javdromero May 04 '21 at 14:51
  • @javdromero Before posting the question, I had changed the value of `colorOnPrimary` to a different one to debug the issue. But the issue is same.. – mufazmi May 04 '21 at 14:54
  • What about the color on the edittexts and the search bar? Is is white also? – javdromero May 04 '21 at 15:02
  • @javdromero The search box is not actually `EditText` it's a `TextView`, When user click on it, I am open another activity. The EditText which is inside `BottomSheetDialog` is default which is black I think – mufazmi May 04 '21 at 15:06
  • `DayNight.DarkActionBar` must be the problem, try to override the color with `@color/your_color` on your theme. Or refer to this [answer](https://stackoverflow.com/a/63279563/15298643) – javdromero May 04 '21 at 15:22

3 Answers3

2

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

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

I also faced with same issue. In my case I used custom toolbar with style.

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ToolbarStyle" />

ToolbarStyle

<style name="ToolbarStyle" parent="AppTheme">
    <item name="android:textColorPrimary">@color/white</item>
</style>

So I changed android:textColorPrimary in ToolbarStyle to black and now it works fine

Shams
  • 388
  • 1
  • 2
  • 13
  • I have followed your answer, but the issue is same. see the result -> https://youtu.be/hCuhSkSgzGg – mufazmi May 04 '21 at 17:19
  • Please change android:textColorPrimary from white to black – Shams May 04 '21 at 17:31
  • Working but it's changed all the text color of toolbar like title and icon, etc. I only want to change the menuItemTextColor. – mufazmi May 05 '21 at 03:16
0

just add this code to your themes.xml and themes.xml(night) files

<item name="android:textColor">@color/white</item>