To show menu icon in action bar as shown in the below image, follow these steps
- Create a menu resource inside res/menu, name it whatever you want.
- Override onCreateOptionsMenu and inflate it.
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.yourentry, menu); return true; }
- Set
android:showAsAction="always
in the layout file.<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_setting" android:icon="@drawable/ic_settings_24px" android:title="@string/setting_text" app:showAsAction="always"/> </menu>
- If an actions in the ActionBar is selected, the
onOptionsItemSelected()
method is called. It receives the selected action as parameter. Based on this information you code can decide what to do for example:@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuitem1: Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT).show(); break; case R.id.menuitem2: Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }