Question

I am using AppCompat library in my Church Application, but my menu icons are not showing up in ActionBar.

public class MainActivity extends ActionBarActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;

         case R.id.menu_item_share:
            shareApp(); 
            return true;  

        case R.id.menu_item_about:
            Intent intentAbout = new Intent(MainActivity.this, AboutActivity.class);
            startActivity(intentAbout);
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }

I have written proper xml, as i read it should be :

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>

     <item 
      android:id="@+id/menu_item_share" 
      android:title="@string/menu_share" 
      android:icon="@drawable/ic_action_share" 
      android:showAsAction="ifRoom" /> 

    <item 
      android:id="@+id/menu_item_about" 
      android:title="@string/menu_about" 
      android:icon="@drawable/ic_action_about" 
      android:showAsAction="ifRoom" />

Note: Pressing the MENU button on my device, they appearing.

Was it helpful?

Solution

Change this

android:showAsAction="never"

to

yourapp:showAsAction="never"

Also use yourapp:showAsAction="always"

You can also use "always" to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way.

Note : If there's not enough room in the action bar for the action item, the menu item appears in the overflow where only the title appears.

Similarly for others.

Quoting docs

This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

You already have

xmlns:yourapp="http://schemas.android.com/apk/res-auto"

For more information read

http://developer.android.com/guide/topics/ui/actionbar.html

OTHER TIPS

Change android:showAsAction="never" to android:showAsAction="always"

If you work with AppCompat action bar for versions below 3.0 yo need this code line in the menu tag inside your menu.xml:

xmlns:custom="http://schemas.android.com/apk/res-auto"

Your menu tag in your menu layout should look like:

<?xml version="1.0" encoding="utf-8"?>
<menu
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res-auto">

  <!-- ITEMS HERE -->

</menu>

Then also change:

android:showAsAction="never"

for:

android:showAsAction="always"

or:

android:showAsAction="ifRoom"

It is also recommended to have all the items in a submenu, so you will have only a menu button on the action bar and then when you press it you see the other menu options in a dropdown menu.

You can achieve this by doing:

 <item
    android:id="@+id/action_submenu"
    android:showAsAction="always"
    android:title=""
    android:icon="@drawable/your_icon" >

    <menu >

      <item />

      <item />

      ........

    </menu>

 </item>

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top