69

i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have:

getMenuInflater().inflate(R.menu.submit_action, menu);

my submit_action is:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_submit"
        android:actionLayout="@layout/check"
        app:showAsAction="always"  />
</menu>

my check layout is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/actionButtonStyle"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#e8e8e8"
    android:clickable="true"
    android:contentDescription="lol" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_tick" />

</RelativeLayout>

but even with all of this setup, this is how the action bar appears, not showing my menuitem at all (but it's there, because it responds to the click, but doesn't appear):

enter image description here

tipu
  • 9,464
  • 15
  • 65
  • 98

4 Answers4

243

Try app:actionLayout="@layout/check" instead of android:actionLayout="@layout/check".

If you're using ActionbarSherlock or AppCompat, the android: namespace will not work for MenuItems. This is because these libraries use custom attributes that mimic the Android APIs since they did not exist in earlier versions of the framework.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Ben Harris
  • 5,664
  • 3
  • 30
  • 27
  • 2
    And by `app:actionLayout` he means the name of your app package. So if your app package name is twitter it would be `twitter:actionLayout`. – b.lyte Jan 13 '15 at 17:59
  • 4
    @clu you can use whatever you want. The default is to use "app" – Philip Giuliani Jul 08 '15 at 14:59
  • 6
    @clu No, app is a defined namespace within the XML. It refers to the applications compatibility libraries. If app doesn't resolve, paste xmlns:app="http://schemas.android.com/apk/res-auto" in your tag.(Yes, old comment, putting more information here to help those that might google). – Demortes Dec 21 '15 at 21:57
  • Don't forget to change to support library 23.1.0 Otherwise the menu xml will let you use actionlayout and not complain, but it won't work. `compile 'com.android.support:support-v4:23.1.0'` – YueMuc Aug 07 '17 at 13:56
  • My layout is always on the right and setting the gravity does not work and how can you use the item without a title? didnt you get a lint error?? – Pemba Tamang Dec 23 '19 at 04:57
  • This is absolutely HORRID. BOTH `android:actionLayout` AND `app:actionLayout`? Wasted hours on this, inflating my own custom layout programmatically, even writing an entire Medium article lamenting that `android:actionLayout` is ignored. – rmirabelle Jan 21 '21 at 19:37
8

when using Appcompact ,menu item will be like

<item android:id="@+id/cart"
    app:actionLayout="@layout/actionbar_cart"
    android:title="@string/action_cart"
    app:showAsAction="always"
 />

Voora Tarun
  • 1,266
  • 1
  • 20
  • 38
6

The answer from Ben Harris is absolutely correct. However, in some case like when using attributes like:

      app:showAsAction="ifRoom|collapseActionView"

used in SearchView (in my case), the layout view isn't shown and that caused a lot of headache to me. It seems that collapseActionView is not supported with action view in appcombat. So consider this as well while doing your stuffs.

neaGaze
  • 1,381
  • 22
  • 28
2

use app namespace instead of android and it will work fine.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_submit"
        app:actionLayout="@layout/check"
        app:showAsAction="always"  />
</menu>
coding.cat3
  • 111
  • 4