Skip to content

Instantly share code, notes, and snippets.

@AkaashSaini
Created July 7, 2021 11:31
Show Gist options
  • Save AkaashSaini/6d830853b86cbb1546e2955bfb16bbfa to your computer and use it in GitHub Desktop.
Save AkaashSaini/6d830853b86cbb1546e2955bfb16bbfa to your computer and use it in GitHub Desktop.
Creating the Menu –
• Create a new menu using
o Create a new android xml file in menu folder with name main_menu.xml
o Write following code in this file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:alphabeticShortcut="s"
android:title="Sweet">
</item>
<item
android:id="@+id/item2"
android:alphabeticShortcut="t"
android:title="Toast">
</item>
</menu>
• Write following code in main java file
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater awesome=getMenuInflater();
awesome.inflate(R.menu.main_menu, menu);
return true;
}
• Override following method
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.item1:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment