Getting SearchView with MenuItemCompat (Android)

I'm trying to implement a SearchView ActionBar element, as Android developers say, but I'm having problems. ( http://developer.android.com/guide/topics/ui/actionbar.html ).

There are two errors that, although I searched a lot, I could not find a solution.

1) I have a problem with the MenuItemCompat class. It says: The getActionView (MenuItem) method is undefined for the MenuItemCompat type

I can use the following methods only for this class:

  • setShowAsAction (item, actionEnum)
  • setActionView (element, view)

Here is the code

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.restloader, menu); MenuItem searchItem = menu.findItem(R.id.search_menu); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); // Configure the search info and add any event listeners return super.onCreateOptionsMenu(menu); } 

2) There is a problem with this: XMLNS: MyApp = "http://schemas.android.com/apk/res-auto" I do not understand why it is used, but if Google talks about it, it should be appropriate.

Error message: Several annotations found on this line: - error: resource identifier found for the attribute "actionViewClass" in the package 'Com.example.pruebahttp3' - error: resource identifier not found for the attribute 'showAsAction' in the package 'Com. example.pruebahttp3 '

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/search_menu" android:orderInCategory="100" android:title="@string/search" android:icon="@drawable/ic_search_category_default" myapp:showAsAction="ifRoom|collapseActionView" myapp:actionViewClass="android.support.v7.widget.SearchView"> </item> 

Thank you very much!

+6
source share
3 answers

I have the same problem, I solved it using the following code. Take care of your namespace.

 <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/abc_ic_search" share:showAsAction="ifRoom" share:actionViewClass="android.support.v7.widget.SearchView" android:title="@string/abc_searchview_description_search" /> 

`

+2
source

For 1st: Fixing the second fix it :)

For the second:

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

Change myapp to your com.xxx.xxx application namespace

0
source

Try copying the lib files directly from your file: \ sdk \ extras \ android \ support \ v7 \ appcompat \ libs I have a similar problem, but it arises for me when I directly copy the JAR library file and not following the Android support library procedure . Try the opposite, it might work for you. With curiosity, if you ask me.

0
source

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


All Articles