I want to add a Button to the Action Bar to the right hand side of Example as in this screen shot:
I get actionBar in onCreate method as:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
and back button(onOptionsItemSelected method) as below:
public boolean onOptionsItemSelected(MenuItem item){
Intent myIntent = new Intent(getApplicationContext(),MainActivity.class);
startActivityForResult(myIntent, 0);
return true;
}
How can I add button?
you have to create an entry inside
res/menu,
overrideonCreateOptionsMenu
and inflate itan entry for the menu could be:
Thanks to @Blackbelt! The new method signature for inflating the menu is this:
An activity populates the ActionBar in its
onCreateOptionsMenu()
method.Instead of using
setcustomview()
, just overrideonCreateOptionsMenu
like this: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: