Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu items doesn't show up on the actionbar [duplicate]

People also ask

How do I hide a menu item in the ActionBar?

You can set a flag/condition. Call invalidateOptionsMenu() when you want to hide the option. This will call onCreateOptionsMenu() .

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

How do I add items to Action Bar?

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.


You seem to be using the wrong menu: Your file is named "menu_activity.xml" and you inflate the menu with the Resource-Id: R.menu.reminder_menu

The Resource name of the menu should be the same as the file name, i.e.: R.menu.manu_activity

Try it with this again - I ran into this too once and it drove me nuts...

Update After clarification that the above part was for obfuscation, please make sure that:

  1. You extend ActionBarActivity.
  2. You use (or extend) one of the Theme.AppCompat themes for the activity (or whole app)
  3. Because on older devices, the Actionbar related attributes are not present, make sure that all these attributes in the XML are used with a custom namespace. Here that would be the showAsAction attribute, so that the compatibility library can pick them up.

You already had the custom namespace defined ("app", in the menu tag). You need to change the android:showAsAction tag to app:showAsAction according to the Android guide.

Your corrected menu_activity.xml would then look like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/menu_lang"
        app:showAsAction="always"
        android:title="@string/menu_lang"
        android:icon="@android:drawable/ic_input_lang"/>

</menu>

The extended Android guide for actionbar covers these new and nasty traps...


Okay I think I found a solution for you. It is called Overflow Menu and you need to call the below method in your onCreate method.

private void getOverflowMenu() {

try {
   ViewConfiguration config = ViewConfiguration.get(this);
   Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
   if(menuKeyField != null) {
       menuKeyField.setAccessible(true);
       menuKeyField.setBoolean(config, false);
   }
} catch (Exception e) {
   e.printStackTrace();
}
}

Try this and let me know if it works for you.

EDIT:

I think I understood your problem finally. I can give you one idea. If you do not want to use overflow menu and just display menu items as displayed in the screen-shot you have posted, you can create a layout on top of your activity.

You can take a linear layout, give a background that looks the same as action bar and place whatever icons you want to put there, and give functionality to them with onClickListener. Hope this will give you some idea. This way you can create your own custom menu. Try the layout below and replace ic_launcher drawable with menu icons. Then just set onClickListeners to them and perform whatever functions you want to perform inside onClick method.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#66000000" >

    <ImageView
        android:id="@+id/xMenuBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/ic_launcher" />
     <TextView
        android:id="@+id/xMenuTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chats"
        android:textSize="18sp"
        android:layout_toRightOf="@+id/xMenuBtn1"
        android:layout_centerVertical="true" />
    <ImageView
        android:id="@+id/xMenuBtn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/xMenuBtn3"
        android:background="@drawable/ic_launcher" />
    <ImageView
        android:id="@+id/xMenuBtn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher" />

</RelativeLayout>