3

I have a search view with a background drawable, but I can't seem to get the text to appear no matter what I try. No text shows up in the SearchView.

I've tried the android:text and android:queryHint and even setting it programmatically using mSearchView.setQueryHintText("search for something"); but none of these work. The searchView is not inside an actionBar.

Here is the xml for the searchview

<android.support.v7.widget.SearchView
        android:id="@+id/m_search_view"
        android:layout_width="match_parent"
        android:layout_height="@dimen/searchbar_height"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toLeftOf="@+id/other_button"
        android:background="@drawable/rounded_corner_box"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical|left"
        android:paddingLeft="@dimen/left_padding"
        android:queryHint="search for something"
        android:textAppearance="@style/m_text_appearance"
        android:textColor="@color/m_text_color" />

When I don't set a background, I don't see the searchView at all (the searchView is translucent).

Any help would be appreciated.

VIN
  • 6,385
  • 7
  • 38
  • 77
  • You are using the SearchView from support library, probably you need to set the properties with app and android, like android:queryHint="search for something" and app:queryHint="search for something" – jonathanrz Aug 24 '16 at 23:40

6 Answers6

12
searchView.setIconified(false);
Rodrigo João Bertotti
  • 5,179
  • 2
  • 23
  • 34
  • why does this work? Search view recieved onClick listener events but did not show keyboard (InputMethod) nor was I able to type a query when forcing the keyboard to be displayed. – CybeX Aug 28 '17 at 07:57
7

In the XML, add android:iconifiedByDefault="false" if you have already set a value for android:queryHint.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
James Chang
  • 608
  • 8
  • 21
7

For android.support.v7.widget.SearchView, instead of using

android:queryHint="your hint" 
android:iconifiedByDefault="false"

you must use

app:queryHint="your hint" 
app:iconifiedByDefault="false"
Andreas
  • 2,455
  • 10
  • 21
  • 24
Selmeny
  • 220
  • 3
  • 11
6

In the xml layout file just add

app:iconifiedByDefault="false"

Notice that it's app: and not android:

Cedriga
  • 3,860
  • 2
  • 28
  • 21
4

Fixed my own issue: I had to dig into the SearchView's source. This is how I got the background and text to show in the searchView:

SearchView searchView = (SearchView)mLayout.findViewById(R.id.m_searchview);
searchView.setQueryHint("search for something");
View searchTextView = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchTextView.setBackground(
ResourcesCompat.getDrawable(getResources(), R.drawable. rounded_corner_box, getTheme()));
View searchFrame = searchView.findViewById(android.support.v7.appcompat.R.id.search_edit_frame);
searchFrame.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.m_background, getTheme()));
VIN
  • 6,385
  • 7
  • 38
  • 77
3

In the xml layout file just add

app:queryHint="My hint"
leodev
  • 208
  • 4
  • 13