How can I add an ImageButton to an Actionbar in Android?
12,553
Solution 1
You should add a custom view to you action bar inside your activity.
See this : https://stackoverflow.com/a/16029214/713778
Solution 2
To add a button to action bar, locate the onCreateOptionsMenu method in the activity with the action bar that you want to add the button to. Next go to res>menu>main and add the item based on this example:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings" //<-- id of the view
android:orderInCategory="100" //<-- not important for the moment (importance)
android:showAsAction="never" //<-- this will always put it in overflow. set always to show as action
android:icon="@drawable/ic_launcher"/> //<-- Icon to display in action bar
android:title="@string/action_settings"/> //<-- not really important
</menu>
next, we will want to listen for item clicks, so add this method to your activity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// action with ID action_settings was selected
case R.id.action_settings:
// this is where you put your own code to do what you want.
break;
default:
break;
}
return true;
}
And there ya go!
Author by
anu_r
Updated on June 25, 2022Comments
-
anu_r almost 2 years
I want to customize my Actionbar. I want to add an ImageButton in Actionbar, which when clicked goes to another activity. My code is as below. I don't know to proceed forward. can anyone suggest me step by step what to do.
final ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(getResources().getDrawable(R.color.blue));