I want to add an action to the action bar, but it appears in my action bar as a drop down list. How can I add button to the action bar? My code is:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="Add"
showAsAction="ifRoom"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
You just simply add the one more item into menu and inflate that menu into your activity .... like below you will add your button and access that button into your activity...
Inflating the menu like below.....
Accessing method in action bar item is....
that's if you have any doubt comment
If you are using
android.app.Activity
simply changeshowAsAction="always"
to"android:showAsAction="always"
.If you are using
android.support.v7.app.Activity
change the code as follows: