0

everyone! I'm a beginner, developing a mobile app. Friends, my menu items are not displayed. I click on three dots clicking on the menu. A white window appears window without menu items. Program code "menu.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"
    app:theme="@style/Theme.MAGIC">

    <item
        android:title="@string/search"
        android:id="@+id/search_option"
        android:icon="@drawable/ic_baseline_search_24"
        app:showAsAction="ifRoom"
        app:actionViewClass="androidx.appcompat.widget.SearchView">
    </item>

    <item
        android:id="@+id/setting_app"
        android:title="@string/menu_settings"
        android:icon="@drawable/ic_settings_app" />

    <item
        android:id="@+id/about_app"
        android:title="@string/menu_about_application"
        android:icon="@drawable/ic_info_about" />
</menu>

Program code "themes.xml":

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/black</item>
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    </style>
</resources>

Program code "MainActivity.java"

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem;
        androidx.appcompat.widget.SearchView searchView;

        getMenuInflater().inflate(R.menu.menu, menu);
        menuItem = menu.findItem(R.id.search_option);

        searchView = (SearchView) menuItem.getActionView();

        searchView.setOnQueryTextListener(this);
        return super.onCreateOptionsMenu(menu);
    }

How do I display the menu items? Friends, help solve the problem! I'm testing the app on a mobile device.

MAGistr
  • 3
  • 3
  • your menu is showing properly but for changing theme setting your menu text is also showing white instead of black. Checkout this answer https://stackoverflow.com/a/25731668/16765223 – M DEV May 25 '22 at 18:38
  • The problem is solved. – MAGistr May 31 '22 at 13:05

2 Answers2

0

When I create my menus, in the onCreateOptionsMenu override method I return true rather than the super.

Also, do you override the onOptionsItemSelected method to handle the clicks? You didn't post that code.

Here is Google's documentation on creating menus: https://developer.android.com/guide/topics/ui/menus

shagberg
  • 2,427
  • 1
  • 12
  • 23
  • Oh, yeah, I forgot. Below is the program code. public boolean onOptionsItemSelected(@NonNull MenuItem item) { if(item.getItemId() == R.id.about_app){ startActivity(new Intent(MainActivity.this, AboutAppActivity.class)); } else if(item.getItemId() == R.id.setting_app){ showDialogBox(); } return true; } – MAGistr May 26 '22 at 08:15
0

The problem was in the XML file. Added the following line: <item name="android:textColor">@color/purple_200</item>. The application menu looks like this:menu

Full program code:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- The basic theme of the application -->
<style name="Theme.MAGIC" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- another part of the code -->

    <!-- Text color, including menu text -->
    <item name="android:textColor">@color/purple_200</item>
</style>
MAGistr
  • 3
  • 3