onCreateOptionsMenu not getting called, i have no clue whats going on
Here is my activity
class BasicLayoutActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_basic_layout)
setSupportActionBar(toolbar)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.menu_basic_layout,menu)
return true
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".BasicLayoutActivity">
<item android:id="@+id/like" android:title="like" app:showAsAction="always"
android:icon="@drawable/ic_insert_emoticon_black_24dp"/>
</menu>
theme applied
<style name="BaseTheme" parent="Theme.MaterialComponents.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
And stackoverflow is showing me error that 'It looks like your post is mostly code, please add some more details :P'
I have created a new project for you and it works as charm.
Just remove setSupportActionBar(toolbar)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.menu_layout,menu)
return true
}
}
Removed include and it is working now
<include
android:id="@+id/includeToolbar"
layout="@layout/toolbar_white"/>
common toolbar
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
style="@style/WhiteToolbarStyle">
<TextView
style="@style/ToolbarTitleStyle"
android:id="@+id/toolbarTitle"
android:text="Basic Layout"/>
</androidx.appcompat.widget.Toolbar>
This is not working, onCreateOptionsmenu not getting called
removed include, directly added to activity layout, now its working May be a bug in Androidx or my include file
Change parent theme to "Theme.AppCompat.Light.DarkActionBar" or "Theme.MaterialComponents.Light.DarkActionBar" it seems you need "ActionBar" to hit "onCreateOptionsMenu"
<style name="BaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
then hide "ActionBar" as below on activity:
supportActionBar?.hide()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.