Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActionView() of my MenuItem return null

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>
like image 586
Waza_Be Avatar asked Jan 11 '13 21:01

Waza_Be


4 Answers

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.

like image 91
Omid Heshmatinia Avatar answered Nov 14 '22 16:11

Omid Heshmatinia


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

like image 29
Gabe Sechan Avatar answered Nov 14 '22 14:11

Gabe Sechan


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"

like image 33
Vito Valov Avatar answered Nov 14 '22 14:11

Vito Valov


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"

like image 7
Ali Raza Avatar answered Nov 14 '22 14:11

Ali Raza