Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to disable Search button, how to implement onSearchRequested()?

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>
like image 299
Noman Arain Avatar asked Oct 31 '12 16:10

Noman Arain


1 Answers

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.

like image 61
Phil Avatar answered Oct 09 '22 20:10

Phil