Add a menu to an empty activity

Viewed 56455

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);
}
4 Answers
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);
    }
}

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
   }
}
Related