Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add button in ActionBar(Android)?

I want to add a Button to the Action Bar to the right hand side of Example as in this screen shot:

a screenshot of an actionbar with no buttons. the title is 'Example'

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?

like image 930
Ponting Avatar asked Jul 02 '13 12:07

Ponting


People also ask

What is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


1 Answers

you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it

@Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater = getMenuInflater();     inflater.inflate(R.menu.yourentry, menu);     return true; } 

an entry for the menu could be:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >     <item         android:id="@+id/action_cart"         android:icon="@drawable/cart"         android:orderInCategory="100"         android:showAsAction="always"/>  </menu> 
like image 94
Blackbelt Avatar answered Sep 22 '22 16:09

Blackbelt