Create an options menu. (Create a menu directory in res folder if it does not exist and create a menu resource file in that directory. Place the menu.xml file code in that file)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_settings"
android:title="@string/menu_settings"/>
<item
android:id="@+id/action_quit"
android:title="@string/menu_quit"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean handled= false;
switch (item.getItemId()){
case R.id.action_settings:
Toast.makeText(MainActivity.this, "Settings clicked", Toast.LENGTH_SHORT).show();
handled = true;
break;
case R.id.action_quit:
handled = true;
finish();
break;
}
return handled;
}