Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a menu to an empty activity

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); } 
like image 702
Craig Gallagher Avatar asked Mar 18 '16 17:03

Craig Gallagher


People also ask

How do I create a menu activity?

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.

How can I add menu button without action bar?

Just add a button on the top right corner of the screen and put the drop down in the layout.

How do I create an empty activity?

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.


2 Answers

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; } 
like image 182
Zaid Qureshi Avatar answered Sep 28 '22 19:09

Zaid Qureshi


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);     } 
like image 28
mazhar Avatar answered Sep 28 '22 20:09

mazhar