27

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);
}
Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52

4 Answers4

40

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;
}
Zaid Qureshi
  • 1,203
  • 1
  • 10
  • 15
14

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);
    }
mazhar
  • 180
  • 1
  • 11
4
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) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId()==R.id.action_Menu){
            Toast.makeText(this, "Action Item", Toast.LENGTH_SHORT).show();
        }

        if (item.getItemId()==R.id.instructions){
            Toast.makeText(this, "Hnstructions Item", Toast.LENGTH_SHORT).show();
        }

        if (item.getItemId()==R.id.hotels){
            Toast.makeText(this, "Hotels Item", Toast.LENGTH_SHORT).show();
        }

        return super.onOptionsItemSelected(item);
    }
}
Dmitriy Fialkovskiy
  • 3,065
  • 8
  • 32
  • 47
0

With the new Material Design Principal you have to setSupportActionBar(toolbar) inside your activity inorder to show menu.

class HomeActivity:BaseActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)
       setContentView(R.layout.activity_home)
       setSupportActionBar(toolbar)
   }

  override fun onCreateOptionsMenu(menu: Menu?): Boolean {
      menuInflater.inflate(R.menu.menu_new,menu)
      return true
   }
}
pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29