I have a SearchView and trying to add a hint text. It seems you can only show the hint when the user actually taps on the SearchView, I googled a lot and tried different approaches I found on StackOverflow such as:
searchView.onActionViewExpanded();
searchView.setIconified(true);
searchView.setQueryHint("Mitarbeiter suchen");
or playing around more with the code above. I also tried adding IconifiedByDefault
in XML file but it was no help.
I'm sure there's a way for it. 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>
Setting iconifiedByDefault
and focusable
properties to 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" />
Checks if searchView is focused or not by 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 the screenshot of my demo app and it is in fragment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With