This repository was archived by the owner on Jun 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Option Menu Binding
Steven Lewi edited this page Apr 9, 2015
·
1 revision
@OptionMenu can be used to automatically inflate option menu of your Activities and Fragments. Put this Annotation on the top of Activity/Fragment class.
@OnMenuItemSelected can be used to bind menu item click/select listener (onOptionsItemSelected(MenuItem item))
See this example below.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}@ContentView(R.layout.activity_main)
@OptionMenu(R.menu.menu_main)
public class MainActivity extends BaseActivity {
@Override
protected void onContentViewCreated() {
}
@OnMenuItemSelected(R.id.action_settings)
public void openSettingsPage() {
// open settings page
}
}How's about that?. It's more simple and save your time.
note : @OptionMenu and @OnMenuItemSelected can be applied to Fragment same way as Activity.