Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView set Expanded by default

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!

like image 619
bernzkie Avatar asked Nov 17 '15 12:11

bernzkie


People also ask

How to set SearchView in Android Studio?

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.

What is iconifiedByDefault in Android?

android:iconifiedByDefault. The default state of the SearchView. android:imeOptions. The IME options to set on the query text field.

How to implement SearchView in ToolBar in Android?

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.

What is setOnQueryTextListener?

Android SearchView setOnQueryTextListener(OnQueryTextListener listener) Sets a listener for user actions within the SearchView.


2 Answers

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

like image 124
bernzkie Avatar answered Oct 09 '22 23:10

bernzkie


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();
like image 11
TharakaNirmana Avatar answered Oct 10 '22 01:10

TharakaNirmana