Skip to content

Instantly share code, notes, and snippets.

@dnkm
Last active August 29, 2015 14:18
Show Gist options
  • Save dnkm/05957538301d0445692a to your computer and use it in GitHub Desktop.
Save dnkm/05957538301d0445692a to your computer and use it in GitHub Desktop.
Android Tutorial Chapter 2 - Action Bar
// enables the "app icon" as the up button
// make sure to declare parent activity in the manifest
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// if minSdkVersion >= 11, use this instead:
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
<!-- Specify actions in XML -->
<!-- res/menu/main_activity_actions.xml -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" /> <!-- appear as action button if there is room, otherwise in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" /> <!-- only appear in the overflow -->
</menu>
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment