viniciuslucas06
viniciuslucas06

Reputation: 137

SearchView Not call onOptionsItemSelected

"Error" Well Strange Other times I've done it and never tive this problem ...

My searchview not call onOptionsItemSelected if i put "appname:actionViewClass="android.support.v7.widget.SearchView" on menu.

Manifest

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
 <activity
            android:name=".DashBoardActivity"
            android:label="">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

dash_board.xml (menu) (if I retreat actionViewClass he calls(onOptionsItemSelected ), but if i put he dont call)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appname="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.servic.DashBoardActivity" >


           <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          appname:showAsAction="always"
          appname:actionViewClass="android.support.v7.widget.SearchView"/>

             <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        appname:showAsAction="always"/>



</menu>

DashBoardActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dash_board, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();
        switch (id)
        {
        case R.id.action_search:
            mSearchView
            .setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        ToastMensagem("Digite um endereço.");
                    } else {
                        BuscarEndereco(mSearchView.getQuery().toString());
                    }
                }



            });
            break;
        }


        return super.onOptionsItemSelected(item);
    }

The error is, not call onOptionsItemSelected, i need manipulate searchview

Upvotes: 4

Views: 2636

Answers (2)

Will
Will

Reputation: 141

Whenever I add collapseActionView to the menu item the action bar title is hidden on search view click. This is the reason why I was searching for a way to leave it as

android:showAsAction="always"

with onOptionsItemSelected funcionality. If anyone finds anything, please let me know! :)

Upvotes: 0

Bhargav Thanki
Bhargav Thanki

Reputation: 4954

I know its too late now,but i am still writing answer if anyone else find it helpful in future. Yesterday i came into same problem and i solved it by changing the following line in menu xml file

appname:showAsAction="always"

to

appname:showAsAction="always|collapseActionView"

you have to add collapseActionView in appname:showAsAction along with alwaysto enable onOptionsItemSelected on searchView

Upvotes: 17

Related Questions