54

I just would like to tweak the View of an ActionBar MenuItem by code.

Unfortunately, it seems that getActionView always return null!

My code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = this.getSupportMenuInflater();
    inflater.inflate(R.menu.folder, menu);
    return super.onCreateOptionsMenu(menu);

}

public boolean onPrepareOptionsMenu(final Menu menu) {
    MenuItem menuFolder = menu.findItem(R.id.menu_folder);
    Log.i("", "* onPrepareOptionsMenu *" + menuFolder);
    Log.i("", "* getActionView *" + menuFolder.getActionView());

Log is:

01-11 22:13:42.884: I/(7893): * onPrepareOptionsMenu *com.actionbarsherlock.internal.view.menu.MenuItemWrapper@41401ac8

01-11 22:13:42.884: I/(7893): * getActionView *null

Thank a lot for any help

Edit:

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

    <item 
          android:id="@+id/menu_folder"
          android:icon="@drawable/ic_menu_archive"
          android:showAsAction="always"/>
</menu>
Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Waza_Be
  • 39,407
  • 49
  • 186
  • 260

6 Answers6

88

you should use

app:actionLayout="@layout/menu_actionbar_basket"

thats the trick if you use

android:actionLayout="@layout/menu_actionbar_basket"

you would always get null exception in default toolbar.

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
38

getActionView() only works if there's a custom actionView from setActionView.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 13
    Is there an alternative to access the view of the MenuItem? – Waza_Be Jan 11 '13 at 21:23
  • Depends. Do you want to tweak just this button, or all buttons? If it's just this button, you should probably set a custom view. If its all buttons, you should probably use styles if at all possible. What changes are you trying to make? – Gabe Sechan Jan 11 '13 at 21:34
  • 4
    I want to put a "badge" on an icon when there are new content in the application, so I need the View to add this little counter – Waza_Be Jan 11 '13 at 21:35
  • 2
    Ok, then you want to create a view for the item and use setActionView to set it. That's the right answer. There may be a hack to do it by getting the action bar and traversing its children looking for the view, but you don't want to do that. – Gabe Sechan Jan 11 '13 at 21:37
  • 5
    There is one moment. I have project with ActionBarSherlock and two menu folders: just menu and menu-v11. In menu-v11 I have menu item with `android:actionLayout="@layout/search_layout"` and it works on all Android 11+ versions. After this I copied this project and use appcompat-v7 instead of ActionBarSherlock (minApiVersion now is 11). And I have two menu folders: menu and menu-v14. Now `getActionView()` returns null! A had to write `menuSearch.setActionView(R.layout.search_layout);`! So check it - in ActionBarSherlock it should works from xml – Alex Zaitsev Jun 29 '14 at 08:58
  • 1
    I have the same problem with AppCompat. I do as @rocknow suggested, `menuSearch.setActionView(R.layout.search)` and it works. – leegor Jan 15 '15 at 01:29
  • 6
    I had the same issue and I my error was that I wasn't properly using the actionLayout xml attribute properly. First of all, you must define a custom namespace in the menu tag like this `xmlns:app="http://schemas.android.com/apk/res-auto"` Then on the menu item you want to have to use the actionLayout attribute like this: `app:actionLayout="@layout/month_picker"` In this case the month_picker layout is a spinner. Doing this makes it unnecessary to use the setActionView method. – Jorge Salas May 25 '15 at 00:50
33

For me the solution that worked is to use app namespace instead of android.

app:actionViewClass="android.support.v7.widget.SearchView"

Don't forget to declare it: xmlns:app="http://schemas.android.com/apk/res-auto"

Vito Valov
  • 1,765
  • 1
  • 19
  • 37
  • You can also use the actionLayout attribute to set custom Layouts. In my case I was using a Spinner which I had defined in its own xml. – Jorge Salas May 25 '15 at 00:55
8

First Solution

This happen may be you not set actionLayout in Menu file so set your actionLayout in menu file

app:actionLayout="@layout/your_action_layout"

Second Solution and the second solution is

from

android:actionLayout="@layout/your_action_layout"

to

app:actionLayout="@layout/your_action_layout"

Ali Raza
  • 21
  • 2
  • 6
6

If your debug build is working without any issues and issue is only with release build then this may be because of proguard configuration. If you have enabled proguard in your application then add below line to proguard-rules.pro

-keep class android.support.v7.widget.SearchView { *; }
Amol Suryawanshi
  • 2,108
  • 21
  • 29
0

in order to get the MenuItem View i use:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.someMenuItem:

            View v = findViewById(R.id.someMenuItem);
            doSomethingWithView(v);

            break;
    }

    return super.onOptionsItemSelected(item);
}
Gil
  • 11
  • 2