Alex Boyko
Alex Boyko

Reputation: 87

Android SearchView not showing text when typing

I realized SearchView like Google says:

Created searchable.xml in res/xml:

<?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"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />

Created menu item in res/menu/second_activity_menu:

<item
    android:id="@+id/second_toolbar_search"
    android:icon="@mipmap/ic_search_black_24dp"
    android:orderInCategory="100"
    android:title="@string/search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView" />

Change my AndroidManifest:

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

And realized my SecondActivity class which extends AppCompatActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.second_activity_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.second_toolbar_search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    return true;
}

As a result I have this:

Toolbar

After clicling on the search icon I have this:

SearchView

When I start typing something, it doesn't show and it doesn't show "clear" icon and mic icon either. But I added them in searchable.xml.

Thanks for any ideas or code.

ADD: maybe the reason is with toolbar style?

ADD 2: toolbar, but I don't think that it is nessesary

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        local:theme="@style/AppTheme" />

Upvotes: 3

Views: 2901

Answers (5)

Zakhar Rodionov
Zakhar Rodionov

Reputation: 1493

    searchView.setOnTouchListener { v, event ->
        if (event.action == MotionEvent.ACTION_UP){
            searchView.isIconified = false
        }
        false
    }

    searchView.setOnClickListener {
        searchView.isIconified = false
    }

Upvotes: 0

Boris Laskov
Boris Laskov

Reputation: 1

I had the same problem, the solution in my case was to set

searchView.setMaxWidth(Integer.MAX_VALUE);

Upvotes: 0

This might not be the case, but in my case there was some other View that was removing the focus from SearchView right after clicking on the search icon, which gave me the same result as your second image.

 override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.activity_main, menu)
    val searchItem = menu.findItem(R.id.action_search)
    val searchView = searchItem.actionView as? SearchView
    searchView?.setOnFocusChangeListener { _, _ ->
        // My ScrollView was requesting focus
    }
    return true
}

Strangely enough this opens a perfectly working virtual keyboard - with input feedback sound and everything - but all that was typed with it doesn't show anywhere on the screen. Even searchView.setOnQueryTextListener() isn't triggered by the input, meaning that the typed text goes to some void and is not used at all.

Using searchView.requestFocus() after all Views were done with their focuses worked for me.

Upvotes: 0

mixel
mixel

Reputation: 25846

Give a toolbar any explicit layout_height:

<android.support.v7.widget.Toolbar
    ...
    android:layout_height="?attr/actionBarSize"
    ...
/>

P.S. I found this answer here and voted to close this question as duplicate.

Upvotes: 3

Reaz Murshed
Reaz Murshed

Reputation: 24211

As far as I can guess, you have your SearchView working perfectly, but its not visible as the icons and the text colour is white. So I would like to tell you about how you can change the icon and text colours of your SearchView.

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.second_activity_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    // Set text colour 
    SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setHintTextColor(Color.WHITE);
    searchAutoComplete.setTextColor(Color.WHITE);

    View searchplate = (View)searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    searchplate.setBackgroundResource(R.drawable.texfield_searchview_holo_light);
    // Clear search or close icon
    ImageView searchCloseIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    searchCloseIcon.setImageResource(R.drawable.clear_search);

    // Voice icon
    ImageView voiceIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
    voiceIcon.setImageResource(R.drawable.abc_ic_voice_search);

    // Search icon
    ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
    searchIcon.setImageResource(R.drawable.abc_ic_search);

    return super.onCreateOptionsMenu(menu);
}

You hard to customize the SearchView from xml. So follow the programmed solution instead.

Upvotes: 0

Related Questions