Reputation: 1
so I've implemented a simple Kotlin program for displaying the options menu and ran it on like every type of emulator, the output does not display the options menu instead it only displays the contents of the activity_main.xml file. here's the entire app code :
activity_main.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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/data_items.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:id="@+id/save"
android:title="SAVE"
android:icon="@android:drawable/ic_menu_save"
app:showAsAction="never"/>
<item
android:id="@+id/camera"
android:title="CAMERA"
android:icon="@android:drawable/ic_menu_camera"
app:showAsAction="never"/>
<item
android:id="@+id/edit"
android:title="EDIT"
android:icon="@android:drawable/ic_menu_edit"
app:showAsAction="never"/>
</menu>
MainActivity.kt :-
package com.yash.menudesigning
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.data_items,menu)
return true
}
}
res/values/themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.MenuDesigning" parent="Theme.Material3.DayNight.NoActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.MenuDesigning" parent="Base.Theme.MenuDesigning" />
</resources>
I've tried running it on both Virtual and Physical devices, also tried different API versions, also checked for errors, cleaned the code, rebuilded, still the output remains the same.
is anything wrong with my Android Studio? please guide me
Upvotes: 0
Views: 459
Reputation: 1
Thank you soo much, it finally did worked, I did the changes in the themes.xml file, and now its displaying correctly, here's the modified code for
res/values/themes.xml->
@color/my_light_primary --><style name="Theme.MenuDesigning" parent="Theme.AppCompat.Light.DarkActionBar" />
Upvotes: 0
Reputation: 150
Try changing the style. You want to see the menu in the ActionBar but are defining a style with a non action bar theme (Theme.Material3.DayNight.NoActionBar
)
Upvotes: 3