I have the following SearchView in a layout file
<android.support.v7.widget.SearchView
android:id="@+id/search_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:queryHint="@string/search"
android:iconifiedByDefault="false"
/>
However when i run my application no hint appears and the searchview is iconified, in code it works my question is is this a bug on the supportlibrary ?
This example demonstrates how do I use SearchView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java.
In this step we open an xml file and add the code for displaying a SearchView and ListView by using its different attributes. In this step we open MainActivity and add the code to initiate SearchView and ListView. In this we create an Animal name list and then set the adapter to fill the data in ListView.
3. iconifiedByDefault: This attribute of searchview is used to set the default or resting state of the search field. You can set a Boolean value for this attribute and default value is true. True value indicates you can iconifies or expands the search view.
6. setOnQueryTextFocusChangeListener (OnFocusChangeListenerlistener): This listener inform when the focus of the query text field changes. In the below code we show the use of setOnQueryTextFocusChangeListener () of SearchView. 7. setOnQueryTextListener (OnQueryTextListenerlistener): It is a user action within the SearchView.
You are missing required namespace. Replace [yourapp]
with your app name in XML layout. Use this namespace for SearchView properties. This approach is necessary if you use views from support library. Btw. you must do the same with menu for AppCompat action bar.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
android:id="@+id/fragment_address_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/global_bg_front"
android:textSize="@dimen/global_text_medium"
android:textColor="@color/global_text_primary"
[yourapp]:queryHint="@string/fragment_address_search_hint"
[yourapp]:iconifiedByDefault="false" />
</LinearLayout>
You can also setup your SearchView programmatically. See my template on GitHub.
private void setupSearchView(SearchView searchView)
{
// search hint
searchView.setQueryHint(getString(R.string.fragment_address_search_hint));
// background
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchPlate.setBackgroundResource(R.drawable.searchview_bg);
// icon
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageResource(R.drawable.searchview_icon);
// clear button
ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.searchview_clear);
// text color
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTextColor(getResources().getColor(R.color.global_text_primary));
searchText.setHintTextColor(getResources().getColor(R.color.global_text_secondary));
}
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