Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

search widget is not working in release apk

I'm having a strange issue(for me at least). my app works fine with debug apk. but when I create release apk then it shows error in searchwidget. I'm using support library 'com.android.support:appcompat-v7:21.0.3'

this is my code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub

    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.searchmenu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchItem = menu.findItem(R.id.searchwidget);
    final SearchView searchView = (SearchView) MenuItemCompat
            .getActionView(searchItem);
    if(null!=searchManager ) {   
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    searchView.setIconifiedByDefault(false);

    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered
            Programs.this.adapter.getFilter().filter(newText);
            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // this is your adapter that will be filtered
            Programs.this.adapter.getFilter().filter(query);
            return true;
        }
    };
    searchView.setOnQueryTextListener(textChangeListener);

    return super.onCreateOptionsMenu(menu);
}

my menu code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item 
    android:title="@string/search"
    android:id="@+id/searchwidget"
    app:showAsAction="always|collapseActionView"
    android:icon="@drawable/ic_action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"></item> 


</menu>

first I thought it is proguard problem therefore i used this

-keepclassmembers class android.support.v7.widget.SearchView{
}

but still same problem.

can anyone explain why this is happening?

like image 481
akshay bhange Avatar asked Jan 15 '15 16:01

akshay bhange


People also ask

How to add Search bar in android xml?

To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.

How to use SearchView in android?

SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.

How to add Search button in android studio?

Invoking the search dialog For instance, you should add a Search button in your Options Menu or UI layout that calls onSearchRequested() . For consistency with the Android system and other apps, you should label your button with the Android Search icon that's available from the Action Bar Icon Pack.


1 Answers

You have to add

-keep class android.support.v7.widget.SearchView { *; }

to proguard-rules.pro file.

Somehow it is related to proguard obfuscation, probably a bug in SearchView. (some version affected, some not)

like image 92
xnagyg Avatar answered Oct 12 '22 12:10

xnagyg