0

I am using appcompat 7 instead of sherlockactionbar. Now i want to add update counter button in actionbar but return null getActionView in onCreateOptionsMenu function. My code is:

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
View count = menu.findItem(R.id.update).getActionView();
TextView notifCount = (TextView) count.findViewById(R.id.textOne);
if(mNotifCount<=0) notifCount.setVisibility(View.GONE);
notifCount.setText(String.valueOf(mNotifCount));
return super.onCreateOptionsMenu(menu);

My menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_example" android:title="@string/action_example"
    app:showAsAction="withText|ifRoom" />
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
<item
    android:title="update"
    android:id="@+id/update"
    android:actionLayout="@layout/feed_update_count"
    app:showAsAction="always|collapseActionView"
    android:orderInCategory="0" />

Behzad Ashrafian
  • 267
  • 5
  • 17
  • Try suggestions in [this](http://stackoverflow.com/a/18557789/1777090) and [this](http://stackoverflow.com/a/18439753/1777090) – MysticMagicϡ Dec 01 '14 at 08:00

2 Answers2

4

Change android:actionLayout to app:actionLayout and use MenuItemCompat.getActionView(menuItem...);

Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
0

You'll likely need to use MenuItemCompat to load the actionview;

SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
Matthew Runo
  • 1,387
  • 3
  • 20
  • 43
  • Tried that, still null. The menu item has attribute: `android:actionViewClass="android.support.v7.widget.SearchView"` – Alex Bitek Jun 29 '15 at 21:48