I'm currently developing a simple apps using Android Studio with the minimum API Level 8, now i currently using android.support.v7.widget.SearchView
in my LinearLayout
in content_main.xml
layout, not on ACTIONBAR
, now the problem is, i'm not able to set the searchView as Expanded
by default, i already look for solution and i only got this:
android:iconifiedByDefault="false"
here is my full code for my searchView
<android.support.v7.widget.SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchView"
android:background="#00000000"
android:iconifiedByDefault="false"
android:layout_weight="1" />
but no luck, it's not working. anyone could help me? any help would be appreciated. Thank you so much!
SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class. For making search field visible, SearchView uses setIconifiedByDefault(false) method.
android:iconifiedByDefault. The default state of the SearchView. android:imeOptions. The IME options to set on the query text field.
Add the Search View to the App Bar To add a SearchView widget to the app bar, create a file named res/menu/options_menu. xml in your project and add the following code to the file. This code defines how to create the search item, such as the icon to use and the title of the item.
Android SearchView setOnQueryTextListener(OnQueryTextListener listener) Sets a listener for user actions within the SearchView.
Ok, so i solve the problem. to anyone who want to use support library for SearchView here's how you can customize it.
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:closeIcon="@drawable/ic_clear" // To set custom clear icon
app:searchIcon="@drawable/ic_search" //To set custom search icon
app:queryBackground="@color/transparent" // i decided to remove underline so i create a custom background for my searchView
app:queryHint="@string/filterHint" //to set a custom Hint
app:iconifiedByDefault="false" //And finally, to Expand it as default
/>
as i said, i'm afraid of overriding xml layout on runtime via java code, so to make it organize and no more overriding during the runtime, this is how i do it.
Reference
Thanks for help!!!
I used the below code to expand the searchview and place the cursor at the same time:
final SearchView sv = new SearchView(((MainActivity) getActivity()).getSupportActionBar().getThemedContext());
sv.setIconifiedByDefault(true);
sv.setFocusable(true);
sv.setIconified(false);
sv.clearFocus();
sv.requestFocusFromTouch();
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