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.
2
Answers
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:menuFull program code:
When I create my menus, in the
onCreateOptionsMenu
override method I returntrue
rather than thesuper
.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