Android, How to create option Menu
public class MenuTest extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.more_tab_menu, menu);
// return true so that the menu pop up is opened
return true;
}
}
and don't forget to press the menu button or icon on Emulator or device
How to create an options menu for an Activity using AndroidX?
Changing the MainActivity.java file constructor line to extend AppCompatActivity
public class MainActivity extends AppCompatActivity {
and setting the android:theme to a variant of Theme.AppCompat* shows the MainActivity Appbar that I've been struggling to show. What follows is some more research I did to find a proper solution.
Why this works... are the differences between AppCompatActivity and the base class Activity. AppCompatActivity inherits from Activity and using one of these themes tells the app to use the native ActionBar class to provide the app bar. Generally, it seems that using the new Toolbar object is the best way to go as it allows for more customization (as of 2019). Most earlier questions revolving around this topic use outdated/deprecated code such as the ActionBarActivity.
This is achieved by changing the Activity's Layout file to include a Toolbar as per this documentation. The theme for the Toolbar can then be modified appropriately using techniques outside of the scope of this question.
Implementing an option menu in Android Studio
In your java code, add this onCreateOptionsMenu
to show optionMenu,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu); //your file name
return super.onCreateOptionsMenu(menu);
}
Keep your under res\menu\option_menu folder,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>
Now, if you want to set onOptionsItemSelected
i.e onClick event for that ou can use,
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.new_game:
//your code
// EX : call intent if you want to swich to other activity
return true;
case R.id.help:
//your code
return true;
default:
return super.onOptionsItemSelected(item);
}
}
How to add Options Menu to Fragment in Android
Call the super method:
Java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}
Kotlin:
override fun void onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater)
}
Put log statements in the code to see if the method is not being called or if the menu is not being amended by your code.
Also ensure you are calling setHasOptionsMenu(boolean)
in onCreate(Bundle)
to notify the fragment that it should participate in options menu handling.
Create Options Menu with custom actionbar/toolbar view
Don't forget to call setSupportActionBar()
in your activity.
Without this call, the associated menu methods are never called because the Activity considers that there is no Toolbar.
How to implement this Option Menu?
You can use ListPopupWindow
to handle your case. See this How to set custom layout to item of menu?
Related Topics
Why "This App Has Been Built with an Incorrect Configuration" Error Occured in Some Phones
Unzip a Zipped File on Sd Card in Android Application
Gradle 7 and Jitpack.Io Runs into Error During Publish
Android Mediarecorder - "Start Failed: -19"
Comparing Two Times in Android
Android: Volley Http Request Custom Header
How to Get Contacts from Native Phonebook in Android
Retrieve String Out of Addvalueeventlistener Firebase
Users Does Not Define No Argument Constructor
Inflate a View/Layout into Another Layout
Java.Lang.Noclassdeffounderror: Android.Support.V7.Appcompat.R$Styleable
Android Custom View Group Delegate Addview
Map Not Getting in Emulator Android API V2
Why the View Keeps Flashing When Using Jetpack Navigation with Compose