Nikolai Nikolai - 12 days ago 9
Android Question

Action buttons move to overflow menu (ActionBar)

My ActionBar has 2 action buttons, when I'm trying to change logo on my ActionBar, it doesn't show. I found the solution: in my Mainactivity class I changed ActionBarActivity(it shows by default) to Activity. Then in manifest.xml I change the Theme from

name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">


to

name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">


the logo appeared, but 2 action buttons moved to overflow menu. The question is: How to move this action buttons back?

My menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_share"
android:title="@string/action_share"
android:icon="@drawable/ic_action_share"
android:orderInCategory="110"
app:showAsAction="always" />
<item android:id="@+id/deleteNote"
android:title="@string/delete"
android:orderInCategory="111"
android:icon="@drawable/ic_action_delete"
app:showAsAction="always" />
</menu>


MainActivity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if (isAddingNote)
{
menu.removeItem(R.id.deleteNote);
menu.removeItem(R.id.action_share);
}
return true;


On this forum I found two close posts but it didn't help me.
Action buttons doesn't show up on Action Bar?
and Actionbar not shown with AppCompat

Answer

As per my understanding to your question you were originally trying to change the icon of the actionBar in which you didn't succeed and end up with other error.

Let me tell you that your menu_main.xml code works perfectly there is no error in that.

There are two possible solutions for you and that depends on your requirements.

  1. If you are developing your app with material design support i.e using Appcompat then use following answer.

    • Change android:Theme.Holo.Light.DarkActionBar to Theme.AppCompat.Light.DarkActionBar again.

    • Change extends Activity to extends ActionBarActivity again.

    • Write this code in your java file to change the logo of the actionbar.

      ActionBar actionBar = getSupportActionBar();
      actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
      actionBar.setIcon(R.drawable.YourLogo);`
      

  1. If you want to implement your app with Holo theme then use following answer.

    • First of all remove Appcompat lib from your project depending on the IDE you are using.
    • Let your activity be extends Activity.
    • Let your theme be android:Theme.Holo.Light.DarkActionBar.
    • Change TargetSDK to below 21. Change compile version to below 21.
    • Replace you main_menu.xml

      <menu xmlns:android="http://schemas.android.com/apk/res/android">
                  <item android:id="@+id/action_share"
                        android:title="share"
                        android:orderInCategory="110"
                        android:showAsAction="always"
                        android:icon="@drawable/ic_action_share" />
                  <item android:id="@+id/deleteNote"
                        android:title="delete"
                        android:orderInCategory="111"
                        android:showAsAction="always"
                        android:icon="@drawable/ic_action_delete" />
        </menu>
      

P.S. I do not understand why you removing menu items from your code. I.e.

    if (isAddingNote)
    {
        menu.removeItem(R.id.deleteNote);
        menu.removeItem(R.id.action_share);
    }