Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ActionBar compatibility: MenuItem.setActionView(View)

I'm using the appcompat7 lib for ActionBar backwards compatibility. Now I have a MenuItem that I retrieve, and then want to set an ImageView myView as its icon.

The way how to do it from API level 11 is:

MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh.setActionView(myView);

For API levels lower than 11 this doesn't work, the second line will show an error. Is there an option to do this in compatibility mode?

like image 474
Terry Avatar asked Dec 05 '13 14:12

Terry


Video Answer


1 Answers

Look at MenuItemCompat: http://developer.android.com/reference/android/support/v4/view/MenuItemCompat.html

There is a static function setActionView(MenuItem item, View view)

So your code should look like:

MenuItem menuItemRefresh = menu.findItem(R.id.refresh);
menuItemRefresh = MenuItemCompat.setActionView(menuItemRefresh, myView);
like image 73
Sprigg Avatar answered Oct 26 '22 18:10

Sprigg