I have made an android application in Android Studio and would like to create a option menu on it. I created it as an empty activity and now realize I would have been better creating a blank activity to get the option menu. Is there anyway to create the option menu in a empty activity. If someone could point me to a tutorial that would be great this is my code so far for my menu.
menu_menu
<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="saveourcar.soc.MainActivity"> <item android:id="@+id/action_Menu" android:orderInCategory="100" android:title="Menu" app:showAsAction="never" > <menu> <item android:id="@+id/instructions" android:title="Instructions" android:icon="@drawable/bg"/> <item android:id="@+id/hotels" android:title="Hotels" android:icon="@drawable/mechanic"/> </menu> </item> </menu>
Main activity
public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_Menu) { return true; } return super.onOptionsItemSelected(item); }
Android Menu Creating a Menu in an Activity To define your own menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements: <menu> : Defines a Menu, which holds all the menu items. <item> : Creates a MenuItem, which represents a single item in a menu.
Just add a button on the top right corner of the screen and put the drop down in the layout.
Step 1: Firstly, click on app > res > layout > Right Click on layout. After that Select New > Activity and choose your Activity as per requirement. Here we choose Blank Activity as shown in figure below. Step 2: After that Customize the Activity in Android Studio.
You need to inflate your menu. These tutorials show how to use menus. So something like this, and choose a better name than menu_menu:
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_menu, menu); return true; }
when you make a menu's layout you need to define it for the Activity you want to place it in. You may do so by:
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater findMenuItems = getMenuInflater(); findMenuItems.inflate(R.menu.main_menu, menu); return super.onCreateOptionsMenu(menu); }
the main_menu
is your menu's layout name, and findMenuItems
is an optional name.
And to make your menu's items clickable for an About menu and exiting the app you'll need this:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.aboutMenuItem: Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class); startActivity(aboutIntent); break; case R.id.exitMenuItem: finish(); break; } return super.onOptionsItemSelected(item); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With