android.support.v4.widget
Class SearchViewCompat

java.lang.Object
  extended by android.support.v4.widget.SearchViewCompat

public class SearchViewCompat
extends Object

Helper for accessing features in SearchView introduced after API level 4 in a backwards compatible fashion.


Nested Class Summary
static class SearchViewCompat.OnCloseListenerCompat
          Callback for closing the query UI.
static class SearchViewCompat.OnQueryTextListenerCompat
          Callbacks for changes to the query text.
 
Method Summary
static CharSequence getQuery(android.view.View searchView)
          Returns the query string currently in the text field.
static boolean isIconified(android.view.View searchView)
          Returns the current iconified state of the SearchView.
static boolean isQueryRefinementEnabled(android.view.View searchView)
          Returns whether query refinement is enabled for all items or only specific ones.
static boolean isSubmitButtonEnabled(android.view.View searchView)
          Returns whether the submit button is enabled when necessary or never displayed.
static android.view.View newSearchView(android.content.Context context)
          Creates a new SearchView.
static void setIconified(android.view.View searchView, boolean iconify)
          Iconifies or expands the SearchView.
static void setImeOptions(android.view.View searchView, int imeOptions)
          Sets the IME options on the query text field.
static void setInputType(android.view.View searchView, int inputType)
          Sets the input type on the query text field.
static void setMaxWidth(android.view.View searchView, int maxpixels)
          Makes the view at most this many pixels wide
static void setOnCloseListener(android.view.View searchView, SearchViewCompat.OnCloseListenerCompat listener)
          Sets a listener to inform when the user closes the SearchView.
static void setOnQueryTextListener(android.view.View searchView, SearchViewCompat.OnQueryTextListenerCompat listener)
          Sets a listener for user actions within the SearchView.
static void setQuery(android.view.View searchView, CharSequence query, boolean submit)
          Sets a query string in the text field and optionally submits the query as well.
static void setQueryHint(android.view.View searchView, CharSequence hint)
          Sets the hint text to display in the query text field.
static void setQueryRefinementEnabled(android.view.View searchView, boolean enable)
          Specifies if a query refinement button should be displayed alongside each suggestion or if it should depend on the flags set in the individual items retrieved from the suggestions provider.
static void setSearchableInfo(android.view.View searchView, android.content.ComponentName searchableComponent)
          Sets the SearchableInfo for this SearchView.
static void setSubmitButtonEnabled(android.view.View searchView, boolean enabled)
          Enables showing a submit button when the query is non-empty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newSearchView

public static android.view.View newSearchView(android.content.Context context)
Creates a new SearchView.

Parameters:
context - The Context the view is running in.
Returns:
A SearchView instance if the class is present on the current platform, null otherwise.

setSearchableInfo

public static void setSearchableInfo(android.view.View searchView,
                                     android.content.ComponentName searchableComponent)
Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used to display labels, hints, suggestions, create intents for launching search results screens and controlling other affordances such as a voice button.

Parameters:
searchView - The SearchView to operate on.
searchableComponent - The application component whose SearchableInfo should be loaded and applied to the SearchView.

setImeOptions

public static void setImeOptions(android.view.View searchView,
                                 int imeOptions)
Sets the IME options on the query text field. This is a no-op if called on pre-Build.VERSION_CODES.ICE_CREAM_SANDWICH platforms.

Parameters:
searchView - The SearchView to operate on.
imeOptions - the options to set on the query text field
See Also:
TextView.setImeOptions(int)

setInputType

public static void setInputType(android.view.View searchView,
                                int inputType)
Sets the input type on the query text field. This is a no-op if called on pre-Build.VERSION_CODES.ICE_CREAM_SANDWICH platforms.

Parameters:
searchView - The SearchView to operate on.
inputType - the input type to set on the query text field
See Also:
TextView.setInputType(int)

setOnQueryTextListener

public static void setOnQueryTextListener(android.view.View searchView,
                                          SearchViewCompat.OnQueryTextListenerCompat listener)
Sets a listener for user actions within the SearchView.

Parameters:
searchView - The SearchView in which to register the listener.
listener - the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query.

setOnCloseListener

public static void setOnCloseListener(android.view.View searchView,
                                      SearchViewCompat.OnCloseListenerCompat listener)
Sets a listener to inform when the user closes the SearchView.

Parameters:
searchView - The SearchView in which to register the listener.
listener - the listener to call when the user closes the SearchView.

getQuery

public static CharSequence getQuery(android.view.View searchView)
Returns the query string currently in the text field.

Parameters:
searchView - The SearchView to operate on.
Returns:
the query string

setQuery

public static void setQuery(android.view.View searchView,
                            CharSequence query,
                            boolean submit)
Sets a query string in the text field and optionally submits the query as well.

Parameters:
searchView - The SearchView to operate on.
query - the query string. This replaces any query text already present in the text field.
submit - whether to submit the query right now or only update the contents of text field.

setQueryHint

public static void setQueryHint(android.view.View searchView,
                                CharSequence hint)
Sets the hint text to display in the query text field. This overrides any hint specified in the SearchableInfo.

Parameters:
searchView - The SearchView to operate on.
hint - the hint text to display

setIconified

public static void setIconified(android.view.View searchView,
                                boolean iconify)
Iconifies or expands the SearchView. Any query text is cleared when iconified. This is a temporary state and does not override the default iconified state set by setIconifiedByDefault(boolean). If the default state is iconified, then a false here will only be valid until the user closes the field. And if the default state is expanded, then a true here will only clear the text field and not close it.

Parameters:
searchView - The SearchView to operate on.
iconify - a true value will collapse the SearchView to an icon, while a false will expand it.

isIconified

public static boolean isIconified(android.view.View searchView)
Returns the current iconified state of the SearchView.

Parameters:
searchView - The SearchView to operate on.
Returns:
true if the SearchView is currently iconified, false if the search field is fully visible.

setSubmitButtonEnabled

public static void setSubmitButtonEnabled(android.view.View searchView,
                                          boolean enabled)
Enables showing a submit button when the query is non-empty. In cases where the SearchView is being used to filter the contents of the current activity and doesn't launch a separate results activity, then the submit button should be disabled.

Parameters:
searchView - The SearchView to operate on.
enabled - true to show a submit button for submitting queries, false if a submit button is not required.

isSubmitButtonEnabled

public static boolean isSubmitButtonEnabled(android.view.View searchView)
Returns whether the submit button is enabled when necessary or never displayed.

Parameters:
searchView - The SearchView to operate on.
Returns:
whether the submit button is enabled automatically when necessary

setQueryRefinementEnabled

public static void setQueryRefinementEnabled(android.view.View searchView,
                                             boolean enable)
Specifies if a query refinement button should be displayed alongside each suggestion or if it should depend on the flags set in the individual items retrieved from the suggestions provider. Clicking on the query refinement button will replace the text in the query text field with the text from the suggestion. This flag only takes effect if a SearchableInfo has been specified with setSearchableInfo(View, ComponentName) and not when using a custom adapter.

Parameters:
searchView - The SearchView to operate on.
enable - true if all items should have a query refinement button, false if only those items that have a query refinement flag set should have the button.
See Also:
SearchManager.SUGGEST_COLUMN_FLAGS, SearchManager.FLAG_QUERY_REFINEMENT

isQueryRefinementEnabled

public static boolean isQueryRefinementEnabled(android.view.View searchView)
Returns whether query refinement is enabled for all items or only specific ones.

Parameters:
searchView - The SearchView to operate on.
Returns:
true if enabled for all items, false otherwise.

setMaxWidth

public static void setMaxWidth(android.view.View searchView,
                               int maxpixels)
Makes the view at most this many pixels wide

Parameters:
searchView - The SearchView to operate on.


Copyright © 2013 Marek Kedzierski. All Rights Reserved.