Evren Yurtesen
Evren Yurtesen

Reputation: 2349

Simple Android SearchView not functioning properly

I am trying to follow the search interface manual from android.com. I have few problems and I am not sure what I did wrong exactly. enter image description here

If you want to look, here is the project as zip file.

1- The android:hint is not visible when I touch search icon. It comes empty. Why? If I set it programmatically it works eg. searchView.setQueryHint(getString(R.string.search_hint)); Why it doesn't work from XML? enter image description here

2- Search field does not get focus and keyboard does not appear automatically when I touch search icon. I need to touch to the search textfield to get focus. I believe it should get focus automatically?

3- When I write something and touch the search icon on the keyboard. I do not see any log entries logged at logcat window. What is missing?

4- (SOLVED) I tried android:iconifiedByDefault="false" in XML and also searchView.setIconifiedByDefault(false); and it is always iconified when I start the application. Why is that happening?

I am guessing I missed something somewhere but I am not sure exactly what...

So below are the steps I followed:

I started a new project with 'Basic Activity' in Android Studio 3. It has a ready toolbar and 'Hello World' in the middle.

Then to xml folder I added the searchable.xml file with contents:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="Search hint text"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

Then changed AndroidManifest.xml file and added an activity:

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

Then created a simple SearchableActivity.class. Just to log some lines.

package com.test.myapplication;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SearchableActivity extends ListActivity {
    private static String TAG = SearchableActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.d(TAG, "handleIntent");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
        }
    }
}

Then added to existing menu_main.xml the necessary item:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.test.myapplication.MainActivity">
    <item android:id="@+id/search"
        android:title="@string/app_name"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.widget.SearchView" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

Update: If I change to app:actionViewClass="android.support.v7.widget.SearchView" and also in MainActivity.class if I import import android.support.v7.widget.SearchView; then when I click on the search icon, it shows default Search... text gets focus automatically. But does NOT show hint from XML and still there are no log entries when I submit any information. enter image description here

UPDATE:

I realized that if I put the intent-filter and the meta part under MainActivity I am getting the search intent inside main activity. But I don't understand why it does not start the SearchableActivity. Because manual page for SearchView clearly says:

When a user executes a search from the search dialog or widget, the system starts your searchable activity and sends it a ACTION_SEARCH intent.

Upvotes: 1

Views: 1900

Answers (2)

Evren Yurtesen
Evren Yurtesen

Reputation: 2349

Apparently all I needed to do was to forward the intent to correct activity from onCreateOptionsMenu()

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (searchManager != null)
    searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

Upvotes: 2

AskNilesh
AskNilesh

Reputation: 69689

Use this

app:actionViewClass="android.support.v7.widget.SearchView"

Instead of this

app:actionViewClass="android.widget.SearchView"

Upvotes: 2

Related Questions