Android ActionBar Compatibility: MenuItem.setActionView (View)

I am using appcompat7 lib for backward compatibility ActionBar. Now I have a MenuItem that I am extracting, and then want to set ImageView myView as an icon.

How to do it from API level 11:

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

For API levels below 11 this does not work, the second line will show an error. Is there any way to do this in compatibility mode?

+6
source share
1 answer

Take a 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 this:

 MenuItem menuItemRefresh = menu.findItem(R.id.refresh); menuItemRefresh = MenuItemCompat.setActionView(menuItemRefresh, myView); 
+24
source

Source: https://habr.com/ru/post/959393/


All Articles