3

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" />
7
  • have you checked the color ? Commented Aug 30, 2016 at 9:49
  • Did you check the text color? It might be white. You need to change the text color to black. Commented Aug 30, 2016 at 10:13
  • @ReazMurshed didn't help Commented Aug 30, 2016 at 10:24
  • @BasilBattikhi changed to black. didn't help Commented Aug 30, 2016 at 10:25
  • @AlexBoyko try my answer and please let me know if that helped. Commented Aug 30, 2016 at 10:25

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

maybe problem with toolbar? maybe I should use different style or use ActionBar. I don't know what's the matter
You can change the color of the Toolbar too to see what happens. The idea is to make the contrast.
changed to black. then I typing text, there's nothing happen in SearchView. and there's no voice icon too
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.

Comments

0

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

searchView.setMaxWidth(Integer.MAX_VALUE);

Comments

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

    searchView.setOnClickListener {
        searchView.isIconified = false
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.