24

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!

bernzkie
  • 1,269
  • 2
  • 16
  • 35
  • Try this: `searchItem.expandActionView();` – activesince93 Nov 17 '15 at 12:27
  • is there any solution without setting it programmatically? i want it to set on layout. :-) thank you!!! – bernzkie Nov 17 '15 at 12:28
  • If setting it in `xml` file is not helping, then you should go with programming part. – activesince93 Nov 17 '15 at 12:30
  • aw, i have a bad feeling on setting it programmatically, i mean, once the app is start, if i'm not wrong the first file would be read is the xml layout? then whatever is in xml layout will be override with java code, i want to make it organize, like, since the first file would be read is xml then i want to set it on xml, so no more overriding will be made. – bernzkie Nov 17 '15 at 12:37
  • anyway, i try your suggested code, but not working? – bernzkie Nov 17 '15 at 12:48
  • Possible duplicate of [Expand and give focus to SearchView automatically](http://stackoverflow.com/questions/11710042/expand-and-give-focus-to-searchview-automatically) – Sufian Nov 09 '16 at 14:26

3 Answers3

64

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!!!

Gary Chen
  • 248
  • 2
  • 14
bernzkie
  • 1,269
  • 2
  • 16
  • 35
11

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();
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69
4

To make the SearchView expanded by default, call setIconifiedByDefault(false) on it when you initialise it (e.g. in onCreate(..) . I've found in most cases this will give it focus automatically, but if not simply call requestFocus() on it too.

Rajan Bhavsar
  • 1,977
  • 11
  • 25