Android SearchView displays tooltip text before user starts typing

I have a SearchView and trying to add tooltip text. It seems you can only show a hint when the user actually clicks on the SearchView, I have searched a lot and searched for different approaches I found on StackOverflow, for example:

searchView.onActionViewExpanded();
searchView.setIconified(true);
searchView.setQueryHint("Mitarbeiter suchen");

      

or more play with the code above. I also tried adding IconifiedByDefault

to the XML file, but it didn't help.

I'm sure there is a way. Can anyone help please? :)

fragment_main.xml

<android.support.v7.widget.SearchView
    android:id="@+id/search_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:queryHint="Mitarbeiter suchen">

</android.support.v7.widget.SearchView>

      

+3


source to share


3 answers


Checks if the searchView is focused or not using the isFocused () method. If matches, then it will clear focus.

searchView = (SearchView) findViewById(R.id.searchView);
searchEditText = (EditText) findViewById(R.id.search_src_text); //SearchView editText
closeButton = (ImageView) findViewById(R.id.search_close_btn); //X button of SearchView

searchView.onActionViewExpanded(); //new Added line
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("Search Here");

if(!searchView.isFocused()) {
    searchView.clearFocus();
}

//Query
searchView.setOnQueryTextListener(new 
           SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            return false;
        }
    });

//This is the your x button
closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Clear the text from EditText view
            searchEditText.setText("");

            //Clear query
            searchView.setQuery("", false);
            searchView.clearFocus();
        }
    });

      



Here is a screenshot of my demo application and it is in the snippet.

0


source


The settings for iconifiedByDefault

and focusable

for false

worked for me.



<android.support.v7.widget.SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    app:iconifiedByDefault="false"
    app:queryHint="test" />

      

+4


source


If you want the search box to always be visible, call setIconifiedByDefault (false)

Programmatically setIconifiedByDefault(false)

In XML  app:iconifiedByDefault="false"

+1


source







All Articles