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.
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;
}
}
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>