In your activity class, override the following methods if they are not there by default:
// create an action bar button
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory.
// If you don't have res/menu, just create a directory named "menu" inside res
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
// handle button activities
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.mybutton) {
// do something here
}
return super.onOptionsItemSelected(item);
}
mymenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/mybutton"
android:title=""
app:showAsAction="always"
android:icon="@drawable/mybuttonicon"
/>
</menu>