I am new to Android programming and wanted to try implementing an options menu in an Android app, so I first created a test app and tried to create an options menu there. I wrote the code and everything looked fine, no error and executed in the AVD but when the app opened the options menu was no where to be found.
I saw a couple of tutorials, went through a lot of Stack Overflow threads for the same problem implemented those changes still no luck - still no options menu.
The MainActivity.java file is :
package com.example.test;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/options_menu_example"
android:textSize="24sp"
android:layout_centerInParent="true" />
</RelativeLayout>
options_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/menu_option_1"
android:title="Option 1"
/>
<item
android:id="@+id/menu_option_2"
android:title="Option 2"/>
</menu>
App Screenshot after executing the above code (using a PIXEL 3 AVD with API 34):
(https://i.sstatic.net/9ZFAi.png)
I would greatly appreciate any help in solving this issue and finding what's wrong with the code so I could note that and not make the same mistake again. Thank you
I checked if all the files where in the right place like if the options_menu.xml file is in the menu directory in res , if there are any typos and a whole lot of small things that could be causing the problem, but everything looked fine still couldn't see the options menu in the app.
For a menu to actually show up, you need to have a toolbar in your activity, and your activity doesn't have one
There are two possible reasons for this
<style name="AppTheme" parent="Theme.AppCompat.Light.**NoActionBar**">
So you could use an activity theme which already has a toolbar
OR
The toolbar xml could be something like so
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
and then you could add it to your activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Referencing this answer for some detailed idea