8

Checked out the following: How to have clicking the phone's search button do nothing?

But it does not work for me. Anybody has any other suggestions? Thank you. Following Phil's suggestion updating the code according to his answer. Shows the bug http://www.youtube.com/watch?v=9lekcH1JAf0&feature=youtu.be

So I have made a sample project. I have tested this on two different phones, Android version 2.3.5 and 4.1, I also tested this on emulator version 2.2. After I click the button show dialog, it shows the progress dialog just fine, and it should continue to do so. But clicking the Search button on the phones and the emulator makes the progress dialog disappear. Here's the class, and manifest follows.

Current behavior is that when the search button is clicked the progress dialog disappears. Expected or needed behavior is to disable the search button, or similarly when the dialog is being shown and the search button is clicked, this click does not make the dialog disappear.

    package com.example.test6;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button showDialogButton;
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showDialogButton = (Button) findViewById(R.id.showDialogButton);
        showDialogButton.setOnClickListener(showDialog);
        context = this;
        startSearch(null, false, null, false);
    }

    private OnClickListener showDialog = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ProgressDialog progressDialog = new ProgressDialog(context);
            progressDialog.show();
        }
    };

    @Override
    public boolean onSearchRequested() {
       Log.d("onSearchRequested", "search key pressed");
       return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test6"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="1"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="Test6" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        </activity>
    </application>

</manifest>

My searchable xml is at: res/xml/searchable.xml according to the reference provided. This is how my searchable.xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:searchSuggestAuthority="dictionary"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:includeInGlobalSearch="true">
</searchable>
Community
  • 1
  • 1
Noman Arain
  • 1,172
  • 4
  • 19
  • 45
  • 1
    Show us your code, that's suggestion #1. – dbDev Oct 31 '12 at 16:26
  • When you say "it doesn't work", do you mean you don't see the logging or the phone still launches the search? – DeeV Nov 02 '12 at 16:32
  • Correct, I don't see logging without putting onSearchRequested() in my onCreate(). And search isn't launched, but my dialogs disappear. – Noman Arain Nov 02 '12 at 16:40
  • @NomanArain Could you clarify? I don't understand. Logs are only when `onSearchRequested()` is in `onCreate()`? – pawelzieba Nov 02 '12 at 18:46
  • If I don't call the onSearchRequested() method from onCreate() then I do not see logs. – Noman Arain Nov 02 '12 at 19:46
  • The code you posted works on all devices i've tested (all running ICS or higher). If this code is in your Activity, your Activity is running AND you press the Search button it's working. Try to see what happens if you use a simulator. Maybe your phone has some strange settings, but without more effort from your side, people will ignore this question. If you try it on a simulator and post the complete Activity code, I'm willing to have another look – Entreco Nov 06 '12 at 20:43
  • Cool, thank you. Let me try it on emulator. – Noman Arain Nov 06 '12 at 20:49
  • I just tried the above code in emulator and on two different phones, when a dialog is showing, clicking the search button makes the dialog disappear. Should I post my complete class? Its 1002 lines long. – Noman Arain Nov 06 '12 at 20:58
  • Managed to post the class. Please let me know what else I should do. Thank you. – Noman Arain Nov 06 '12 at 21:40
  • You should strip down your code to just to deal with your problem. We barely need 1002 lines of code if you ask about search button (definitely not for +50 reputation :) – Marcin Orlowski Nov 06 '12 at 23:52
  • @WebnetMobilie.com I think I have done what you have asked me to. Thank you for the suggestion and help. – Noman Arain Nov 07 '12 at 15:44
  • Hope this helps, http://stackoverflow.com/questions/9832381/disable-the-search-button-in-android – Arham Nov 07 '12 at 17:16
  • That's not what I am looking for. I don't want to have to change my dialogs. I have roughly 8 or so dialogs, and this code is not even written by me. It is not a good idea for me to go change each dialog. I want to disable the search button globally. So this doesn't help. – Noman Arain Nov 07 '12 at 17:23

4 Answers4

4

EDIT

Glad my code below was helpful. With your updated question (along with the video), I now believe this is the answer you need: Prevent ProgressDialog from being dismissed when I click the search button (Android)

--PRE-EDIT--

Instead of calling onSearchRequested() from onCreate(), you need to call:

startSearch(null, false, null, false);

Then, to prevent the search button from using the onSearchRequested() method, you override it and return false:

@Override
public boolean onSearchRequested()
{
   return false;
}

This solution is directly discussed in the onSearchRequested documentation:

You can use this function as a simple way to launch the search UI, in response to a menu item, search button, or other widgets within your activity. Unless overidden, calling this function is the same as calling startSearch(null, false, null, false), which launches search for the current activity as specified in its manifest, see SearchManager.

You can override this function to force global search, e.g. in response to a dedicated search key, or to block search entirely (by simply returning false).

Your AndroidManifest.xml is also incomplete. You need to show that your Activity is searchable. Change your MainActivity declaration as follows:

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Note that @xml/searchable is an xml reference in your apk that defines how to search. This resource is discussed in detail here.

Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
0
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if (keyCode == KeyEvent.KEYCODE_SEARCH) {
          Log.d("onKeyDown", "search key pressed");
          // onSearchRequested();
          return true; // You need to return true here.  This tells android that you consumed the event.
       }
       return false;
    }
Jason Hessley
  • 1,608
  • 10
  • 8
0
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
            return true;
        }
        return false; 
    }

http://developer.android.com/reference/android/view/KeyEvent.html

Arham
  • 2,072
  • 2
  • 18
  • 30
0

You can't interrupt the search key event in your activity.

you must interrupt the search key event in your dialog.

    private OnClickListener showDialog = new OnClickListener() {

    @Override
    public void onClick(View v) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setOnKeyListener(new OnKeyListener(){
               public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)     {
               if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                     Log.d("onKeyDown", "search key pressed");
                     return true; 
               }
               return super.onKey(dialog,keyCode,event);
     }});
        progressDialog.show();
    }
};
landry
  • 561
  • 1
  • 7
  • 19
  • I don't know if I am not understanding this? http://developer.android.com/reference/android/app/Activity.html#onSearchRequested%28%29 "You can override this function to force global search, e.g. in response to a dedicated search key, or to block search entirely (by simply returning false)." Search for onSearchRequested in the above page. – Noman Arain Nov 08 '12 at 15:50
  • @Noman Arain,did you have a try with my suggestion? This is about keyevent dispatching. The ProgressDialog is also a window. So it has keydispatching itself. so you should interrupt the search key in dialog instead of in the activity. – landry Nov 09 '12 at 01:56