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?
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With