Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright (C) 2010 The Android Open Source Project
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.actionbarsherlock.widget;
  
  import android.net.Uri;
  import android.os.Build;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  
  
  import static com.actionbarsherlock.widget.SuggestionsAdapter.getColumnString;

A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

When the SearchView is used in an ActionBar as an action view for a collapsible menu item, it needs to be set to iconified by default using setIconifiedByDefault(true). This is the default, so nothing needs to be done.

If you want the search field to always be visible, then call setIconifiedByDefault(false).

Developer Guides

For information about using SearchView, read the Search developer guide.

See also:
android.view.MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
Attr:
ref android.R.styleable#SearchView_iconifiedByDefault
Attr:
ref android.R.styleable#SearchView_imeOptions
Attr:
ref android.R.styleable#SearchView_inputType
Attr:
ref android.R.styleable#SearchView_maxWidth
Attr:
ref android.R.styleable#SearchView_queryHint
 
 public class SearchView extends LinearLayout implements CollapsibleActionView {
 
     private static final boolean DBG = false;
     private static final String LOG_TAG = "SearchView";

    
Private constant for removing the microphone in the keyboard.
 
     private static final String IME_OPTION_NO_MICROPHONE = "nm";
 
 
     private boolean mIconifiedByDefault;
     private boolean mIconified;
     private View mSearchButton;
     private View mSubmitButton;
     private View mSearchPlate;
     private View mSubmitArea;
     private ImageView mCloseButton;
     private View mSearchEditFrame;
     private View mVoiceButton;
     private View mDropDownAnchor;
     private ImageView mSearchHintIcon;
     private boolean mSubmitButtonEnabled;
     private CharSequence mQueryHint;
     private boolean mQueryRefinement;
     private boolean mClearingFocus;
     private int mMaxWidth;
     private boolean mVoiceButtonEnabled;
     private CharSequence mOldQueryText;
     private CharSequence mUserQuery;
     private boolean mExpandedInActionView;
     private int mCollapsedImeOptions;
 
     private SearchableInfo mSearchable;
     private Bundle mAppSearchData;
 
     /*
     * SearchView can be set expanded before the IME is ready to be shown during
     * initial UI setup. The show operation is asynchronous to account for this.
     */
     private Runnable mShowImeRunnable = new Runnable() {
         public void run() {
             InputMethodManager imm = (InputMethodManager)
                     getContext().getSystemService(.);
 
             if (imm != null) {
                 showSoftInputUnchecked(SearchView.thisimm, 0);
             }
         }
     };
 
     private Runnable mUpdateDrawableStateRunnable = new Runnable() {
         public void run() {
             updateFocusedState();
         }
     };
 
     private Runnable mReleaseCursorRunnable = new Runnable() {
         public void run() {
             if ( != null &&  instanceof SuggestionsAdapter) {
                 .changeCursor(null);
             }
         }
     };
 
     // For voice searching
     private final Intent mVoiceWebSearchIntent;
     private final Intent mVoiceAppSearchIntent;
 
     // A weak map of drawables we've gotten from other packages, so we don't load them
     // more than once.
             new WeakHashMap<StringDrawable.ConstantState>();

    
Callbacks for changes to the query text.
 
     public interface OnQueryTextListener {

        
Called when the user submits the query. This could be due to a key press on the keyboard or due to pressing a submit button. The listener can override the standard behavior by returning true to indicate that it has handled the submit request. Otherwise return false to let the SearchView handle the submission by launching any associated intent.

Parameters:
query the query text that is to be submitted
Returns:
true if the query has been handled by the listener, false to let the SearchView perform the default action.
 
         boolean onQueryTextSubmit(String query);

        
Called when the query text is changed by the user.

Parameters:
newText the new content of the query text field.
Returns:
false if the SearchView should perform the default action of showing any suggestions if available, true if the action was handled by the listener.
 
         boolean onQueryTextChange(String newText);
     }
 
     public interface OnCloseListener {

        
The user is attempting to close the SearchView.

Returns:
true if the listener wants to override the default behavior of clearing the text field and dismissing it, false otherwise.
 
         boolean onClose();
     }

    
Callback interface for selection events on suggestions. These callbacks are only relevant when a SearchableInfo has been specified by SearchView.setSearchableInfo(android.app.SearchableInfo).
 
     public interface OnSuggestionListener {

        
Called when a suggestion was selected by navigating to it.

Parameters:
position the absolute position in the list of suggestions.
Returns:
true if the listener handles the event and wants to override the default behavior of possibly rewriting the query based on the selected item, false otherwise.
 
         boolean onSuggestionSelect(int position);

        
Called when a suggestion was clicked.

Parameters:
position the absolute position of the clicked item in the list of suggestions.
Returns:
true if the listener handles the event and wants to override the default behavior of launching any intent or submitting a search query specified on that item. Return false otherwise.
 
         boolean onSuggestionClick(int position);
     }
 
     public SearchView(Context context) {
         this(contextnull);
     }
 
     public SearchView(Context contextAttributeSet attrs) {
         super(contextattrs);
 
         if (.. < ..) {
             throw new IllegalStateException("SearchView is API 8+ only.");
         }
 
         LayoutInflater inflater = (LayoutInflatercontext
                 .getSystemService(.);
         inflater.inflate(..thistrue);
 
         .setSearchView(this);
 
 
 
         // Inform any listener of focus changes
 
             public void onFocusChange(View vboolean hasFocus) {
                 if ( != null) {
                     .onFocusChange(SearchView.thishasFocus);
                 }
             }
         });
 
         TypedArray a = context.obtainStyledAttributes(attrs.., 0, 0);
         int maxWidth = a.getDimensionPixelSize(.., -1);
         if (maxWidth != -1) {
             setMaxWidth(maxWidth);
         }
         CharSequence queryHint = a.getText(..);
         if (!TextUtils.isEmpty(queryHint)) {
             setQueryHint(queryHint);
         }
         int imeOptions = a.getInt(.., -1);
         if (imeOptions != -1) {
             setImeOptions(imeOptions);
         }
         int inputType = a.getInt(.., -1);
         if (inputType != -1) {
             setInputType(inputType);
         }
 
         a.recycle();
 
         boolean focusable = true;
 
         a = context.obtainStyledAttributes(attrs.., 0, 0);
         focusable = a.getBoolean(..focusable);
         a.recycle();
         setFocusable(focusable);
 
         // Save voice intent for later queries/launching
 
 
         if ( != null) {
             if (.. >= ..) {
                 .addOnLayoutChangeListener(new OnLayoutChangeListener() {
                     @Override
                     public void onLayoutChange(View vint leftint topint rightint bottom,
                                                                          int oldLeftint oldTopint oldRightint oldBottom) {
                         adjustDropDownSizeAndPosition();
                     }
                 });
             } else {
                     @Override public void onGlobalLayout() {
                         adjustDropDownSizeAndPosition();
                     }
                 });
             }
         }
 
         updateQueryHint();
     }

    
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:
searchable a SearchableInfo can be retrieved from the SearchManager, for a specific activity or a global search provider.
 
     public void setSearchableInfo(SearchableInfo searchable) {
          = searchable;
         if ( != null) {
             updateSearchAutoComplete();
             updateQueryHint();
         }
         // Cache the voice search capability
          = hasVoiceSearch();
 
         if () {
             // Disable the microphone on the keyboard, as a mic is displayed near the text box
             // TODO: use imeOptions to disable voice input when the new API will be available
         }
     }

    
Sets the APP_DATA for legacy SearchDialog use.

Parameters:
appSearchData bundle provided by the app when launching the search dialog
Hide:
 
     public void setAppSearchData(Bundle appSearchData) {
          = appSearchData;
     }

    
Sets the IME options on the query text field.

Parameters:
imeOptions the options to set on the query text field
See also:
android.widget.TextView.setImeOptions(int)
Attr:
ref android.R.styleable#SearchView_imeOptions
 
     public void setImeOptions(int imeOptions) {
         .setImeOptions(imeOptions);
     }

    
Returns the IME options set on the query text field.

Returns:
the ime options
See also:
android.widget.TextView.setImeOptions(int)
Attr:
ref android.R.styleable#SearchView_imeOptions
 
     public int getImeOptions() {
         return .getImeOptions();
     }

    
Sets the input type on the query text field.

Parameters:
inputType the input type to set on the query text field
See also:
android.widget.TextView.setInputType(int)
Attr:
ref android.R.styleable#SearchView_inputType
 
     public void setInputType(int inputType) {
         .setInputType(inputType);
     }

    
Returns the input type set on the query text field.

Returns:
the input type
Attr:
ref android.R.styleable#SearchView_inputType
 
     public int getInputType() {
         return .getInputType();
     }

    

Hide:
 
     @Override
     public boolean requestFocus(int directionRect previouslyFocusedRect) {
         // Don't accept focus if in the middle of clearing focus
         if (return false;
         // Check if SearchView is focusable.
         if (!isFocusable()) return false;
         // If it is not iconified, then give the focus to the text field
         if (!isIconified()) {
             boolean result = .requestFocus(directionpreviouslyFocusedRect);
             if (result) {
                 updateViewsVisibility(false);
             }
             return result;
         } else {
             return super.requestFocus(directionpreviouslyFocusedRect);
         }
     }

    

Hide:
 
     @Override
     public void clearFocus() {
          = true;
         setImeVisibility(false);
         super.clearFocus();
         .clearFocus();
          = false;
     }

    
Sets a listener for user actions within the SearchView.

Parameters:
listener the listener object that receives callbacks when the user performs actions in the SearchView such as clicking on buttons or typing a query.
 
     public void setOnQueryTextListener(OnQueryTextListener listener) {
          = listener;
     }

    
Sets a listener to inform when the user closes the SearchView.

Parameters:
listener the listener to call when the user closes the SearchView.
 
     public void setOnCloseListener(OnCloseListener listener) {
          = listener;
     }

    
Sets a listener to inform when the focus of the query text field changes.

Parameters:
listener the listener to inform of focus changes.
 
     public void setOnQueryTextFocusChangeListener(OnFocusChangeListener listener) {
          = listener;
     }

    
Sets a listener to inform when a suggestion is focused or clicked.

Parameters:
listener the listener to inform of suggestion selection events.
 
     public void setOnSuggestionListener(OnSuggestionListener listener) {
          = listener;
     }

    
Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

Parameters:
listener the listener to inform when the search button is clicked or the text field is programmatically de-iconified.
 
     public void setOnSearchClickListener(OnClickListener listener) {
          = listener;
     }

    
Returns the query string currently in the text field.

Returns:
the query string
 
     public CharSequence getQuery() {
         return .getText();
     }

    
Sets a query string in the text field and optionally submits the query as well.

Parameters:
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.
 
     public void setQuery(CharSequence queryboolean submit) {
         .setText(query);
         if (query != null) {
             .setSelection(.length());
              = query;
         }
 
         // If the query is not empty and submit is requested, submit the query
         if (submit && !TextUtils.isEmpty(query)) {
             onSubmitQuery();
         }
     }

    
Sets the hint text to display in the query text field. This overrides any hint specified in the SearchableInfo.

Parameters:
hint the hint text to display
Attr:
ref android.R.styleable#SearchView_queryHint
 
     public void setQueryHint(CharSequence hint) {
          = hint;
         updateQueryHint();
     }

    
Gets the hint text to display in the query text field.

Returns:
the query hint text, if specified, null otherwise.
Attr:
ref android.R.styleable#SearchView_queryHint
 
     public CharSequence getQueryHint() {
         if ( != null) {
             return ;
         } else if ( != null) {
             CharSequence hint = null;
             int hintId = .getHintId();
             if (hintId != 0) {
                 hint = getContext().getString(hintId);
             }
             return hint;
         }
         return null;
     }

    
Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.

The default value is true.

Parameters:
iconified whether the search field should be iconified by default
Attr:
ref android.R.styleable#SearchView_iconifiedByDefault
 
     public void setIconifiedByDefault(boolean iconified) {
         if ( == iconifiedreturn;
          = iconified;
         updateViewsVisibility(iconified);
         updateQueryHint();
     }

    
Returns the default iconified state of the search field.

Returns:
Attr:
ref android.R.styleable#SearchView_iconifiedByDefault
 
     public boolean isIconfiedByDefault() {
         return ;
     }

    
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:
iconify a true value will collapse the SearchView to an icon, while a false will expand it.
 
     public void setIconified(boolean iconify) {
         if (iconify) {
             onCloseClicked();
         } else {
             onSearchClicked();
         }
     }

    
Returns the current iconified state of the SearchView.

Returns:
true if the SearchView is currently iconified, false if the search field is fully visible.
 
     public boolean isIconified() {
         return ;
     }

    
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:
enabled true to show a submit button for submitting queries, false if a submit button is not required.
 
     public void setSubmitButtonEnabled(boolean enabled) {
          = enabled;
     }

    
Returns whether the submit button is enabled when necessary or never displayed.

Returns:
whether the submit button is enabled automatically when necessary
 
     public boolean isSubmitButtonEnabled() {
         return ;
     }

    
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(android.app.SearchableInfo) and not when using a custom adapter.

Parameters:
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:
android.app.SearchManager.SUGGEST_COLUMN_FLAGS
android.app.SearchManager.FLAG_QUERY_REFINEMENT
 
     public void setQueryRefinementEnabled(boolean enable) {
          = enable;
         if ( instanceof SuggestionsAdapter) {
                     enable ? . : .);
         }
     }

    
Returns whether query refinement is enabled for all items or only specific ones.

Returns:
true if enabled for all items, false otherwise.
 
     public boolean isQueryRefinementEnabled() {
         return ;
     }

    
You can set a custom adapter if you wish. Otherwise the default adapter is used to display the suggestions from the suggestions provider associated with the SearchableInfo.

 
     public void setSuggestionsAdapter(CursorAdapter adapter) {
          = adapter;
 
     }

    
Returns the adapter used for suggestions, if any.

Returns:
the suggestions adapter
 
     public CursorAdapter getSuggestionsAdapter() {
         return ;
     }

    
Makes the view at most this many pixels wide

Attr:
ref android.R.styleable#SearchView_maxWidth
 
     public void setMaxWidth(int maxpixels) {
          = maxpixels;
 
         requestLayout();
     }

    
Gets the specified maximum width in pixels, if set. Returns zero if no maximum width was specified.

Returns:
the maximum width of the view
Attr:
ref android.R.styleable#SearchView_maxWidth
 
     public int getMaxWidth() {
         return ;
     }
 
     @Override
     protected void onMeasure(int widthMeasureSpecint heightMeasureSpec) {
         // Let the standard measurements take effect in iconified state.
         if (isIconified()) {
             super.onMeasure(widthMeasureSpecheightMeasureSpec);
             return;
         }
 
         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
         int width = MeasureSpec.getSize(widthMeasureSpec);
 
         switch (widthMode) {
             case .:
                 // If there is an upper limit, don't exceed maximum width (explicit or implicit)
                 if ( > 0) {
                     width = Math.min(width);
                 } else {
                     width = Math.min(getPreferredWidth(), width);
                 }
                 break;
             case .:
                 // If an exact width is specified, still don't exceed any specified maximum width
                 if ( > 0) {
                     width = Math.min(width);
                 }
                 break;
             case .:
                 // Use maximum width, if specified, else preferred width
                 width =  > 0 ?  : getPreferredWidth();
                 break;
         }
         widthMode = .;
         super.onMeasure(MeasureSpec.makeMeasureSpec(widthwidthMode), heightMeasureSpec);
     }
 
     private int getPreferredWidth() {
         return getContext().getResources()
     }
 
     private void updateViewsVisibility(final boolean collapsed) {
          = collapsed;
         // Visibility of views that are visible when collapsed
         final int visCollapsed = collapsed ?  : ;
         // Is there text in the query
         final boolean hasText = !TextUtils.isEmpty(.getText());
 
         .setVisibility(visCollapsed);
         updateSubmitButton(hasText);
         .setVisibility(collapsed ?  : );
         updateCloseButton();
         updateVoiceButton(!hasText);
         updateSubmitArea();
     }
 
     private boolean hasVoiceSearch() {
         if ( != null && .getVoiceSearchEnabled()) {
             Intent testIntent = null;
             if (.getVoiceSearchLaunchWebSearch()) {
                 testIntent = ;
             } else if (.getVoiceSearchLaunchRecognizer()) {
                 testIntent = ;
             }
             if (testIntent != null) {
                 ResolveInfo ri = getContext().getPackageManager().resolveActivity(testIntent,
                         .);
                 return ri != null;
             }
         }
         return false;
     }
 
     private boolean isSubmitAreaEnabled() {
         return ( || ) && !isIconified();
     }
 
     private void updateSubmitButton(boolean hasText) {
         int visibility = ;
         if ( && isSubmitAreaEnabled() && hasFocus()
                 && (hasText || !)) {
             visibility = ;
         }
         .setVisibility(visibility);
     }
 
     private void updateSubmitArea() {
         int visibility = ;
         if (isSubmitAreaEnabled()
                 && (.getVisibility() == 
                         || .getVisibility() == )) {
             visibility = ;
         }
         .setVisibility(visibility);
     }
 
     private void updateCloseButton() {
         final boolean hasText = !TextUtils.isEmpty(.getText());
         // Should we show the close button? It is not shown if there's no focus,
         // field is not iconified by default and there is no text in it.
         final boolean showClose = hasText || ( && !);
         .setVisibility(showClose ?  : );
     }
 
     private void postUpdateFocusedState() {
     }
 
     private void updateFocusedState() {
         boolean focused = .hasFocus();
         invalidate();
     }
 
     @Override
     protected void onDetachedFromWindow() {
         post();
         super.onDetachedFromWindow();
     }
 
     private void setImeVisibility(final boolean visible) {
         if (visible) {
             post();
         } else {
             removeCallbacks();
             InputMethodManager imm = (InputMethodManager)
                     getContext().getSystemService(.);
 
             if (imm != null) {
                 imm.hideSoftInputFromWindow(getWindowToken(), 0);
             }
         }
     }

    
Called by the SuggestionsAdapter

Hide:
 
     /* package */void onQueryRefine(CharSequence queryText) {
         setQuery(queryText);
     }
 
     private final OnClickListener mOnClickListener = new OnClickListener() {
 
         public void onClick(View v) {
             if (v == ) {
                 onSearchClicked();
             } else if (v == ) {
                 onCloseClicked();
             } else if (v == ) {
                 onSubmitQuery();
             } else if (v == ) {
                 onVoiceClicked();
             } else if (v == ) {
                 forceSuggestionQuery();
             }
         }
     };

    
Handles the key down event for dealing with action keys.

Parameters:
keyCode This is the keycode of the typed key, and is the same value as found in the KeyEvent parameter.
event The complete event record for the typed key
Returns:
true if the event was handled here, or false if not.
 
     @Override
     public boolean onKeyDown(int keyCodeKeyEvent event) {
         if ( == null) {
             return false;
         }
 
         // if it's an action specified by the searchable activity, launch the
         // entered query with the action key
         // TODO SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
         // TODO if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) {
         // TODO     launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mQueryTextView.getText()
         // TODO             .toString());
         // TODO     return true;
         // TODO }
 
         return super.onKeyDown(keyCodeevent);
     }

    
React to the user typing "enter" or other hardwired keys while typing in the search box. This handles these special keys while the edit box has focus.
 
         public boolean onKey(View vint keyCodeKeyEvent event) {
             // guard against possible race conditions
             if ( == null) {
                 return false;
             }
 
             if () {
                 Log.d("mTextListener.onKey(" + keyCode + "," + event + "), selection: "
                         + .getListSelection());
             }
 
             // If a suggestion is selected, handle enter, search key, and action keys
             // as presses on the selected suggestion
             if (.isPopupShowing()
                     && .getListSelection() != .) {
                 return onSuggestionsKey(vkeyCodeevent);
             }
 
             // If there is text in the query box, handle enter, and action keys
             // The search key is handled by the dialog's onKeyDown().
             if (!.isEmpty() && KeyEventCompat.hasNoModifiers(event)) {
                 if (event.getAction() == .) {
                     if (keyCode == .) {
                         v.cancelLongPress();
 
                         // Launch as a regular search.
                         launchQuerySearch(.null.getText()
                                 .toString());
                         return true;
                     }
                 }
                 if (event.getAction() == .) {
                     // TODO SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
                     // TODO if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) {
                     // TODO     launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mQueryTextView
                     // TODO             .getText().toString());
                     // TODO     return true;
                     // TODO }
                 }
             }
             return false;
         }
     };

    
React to the user typing while in the suggestions list. First, check for action keys. If not handled, try refocusing regular characters into the EditText.
 
     private boolean onSuggestionsKey(View vint keyCodeKeyEvent event) {
         // guard against possible race conditions (late arrival after dismiss)
         if ( == null) {
             return false;
         }
         if ( == null) {
             return false;
         }
         if (event.getAction() == . && KeyEventCompat.hasNoModifiers(event)) {
             // First, check for enter or search (both of which we'll treat as a
             // "click")
             if (keyCode == . || keyCode == .
                     || keyCode == .) {
                 int position = .getListSelection();
                 return onItemClicked(position.null);
             }
 
             // Next, check for left/right moves, which we use to "return" the
             // user to the edit view
             if (keyCode == . || keyCode == .) {
                 // give "focus" to text editor, with cursor at the beginning if
                 // left key, at end if right key
                 // TODO: Reverse left/right for right-to-left languages, e.g.
                 // Arabic
                 int selPoint = (keyCode == .) ? 0 : 
                         .length();
                .setSelection(selPoint);
                .setListSelection(0);
                .clearListSelection();
                ensureImeVisible(true);
                return true;
            }
            // Next, check for an "up and out" move
            if (keyCode == . && 0 == .getListSelection()) {
                // TODO: restoreUserQuery();
                // let ACTV complete the move
                return false;
            }
            // Next, check for an "action key"
            // TODO SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
            // TODO if ((actionKey != null)
            // TODO         && ((actionKey.getSuggestActionMsg() != null) || (actionKey
            // TODO                 .getSuggestActionMsgColumn() != null))) {
            // TODO     // launch suggestion using action key column
            // TODO     int position = mQueryTextView.getListSelection();
            // TODO     if (position != ListView.INVALID_POSITION) {
            // TODO         Cursor c = mSuggestionsAdapter.getCursor();
            // TODO         if (c.moveToPosition(position)) {
            // TODO             final String actionMsg = getActionKeyMessage(c, actionKey);
            // TODO             if (actionMsg != null && (actionMsg.length() > 0)) {
            // TODO                 return onItemClicked(position, keyCode, actionMsg);
            // TODO             }
            // TODO         }
            // TODO     }
            // TODO }
        }
        return false;
    }

    
For a given suggestion and a given cursor row, get the action message. If not provided by the specific row/column, also check for a single definition (for the action key).

Parameters:
c The cursor providing suggestions
actionKey The actionkey record being examined
Returns:
Returns a string, or null if no action key message for this suggestion
    // TODO private static String getActionKeyMessage(Cursor c, SearchableInfo.ActionKeyInfo actionKey) {
    // TODO     String result = null;
    // TODO     // check first in the cursor data, for a suggestion-specific message
    // TODO     final String column = actionKey.getSuggestActionMsgColumn();
    // TODO     if (column != null) {
    // TODO         result = SuggestionsAdapter.getColumnString(c, column);
    // TODO     }
    // TODO     // If the cursor didn't give us a message, see if there's a single
    // TODO     // message defined
    // TODO     // for the actionkey (for all suggestions)
    // TODO     if (result == null) {
    // TODO         result = actionKey.getSuggestActionMsg();
    // TODO     }
    // TODO     return result;
    // TODO }
    private int getSearchIconId() {
        TypedValue outValue = new TypedValue();
                outValuetrue);
        return outValue.resourceId;
    }
    private CharSequence getDecoratedHint(CharSequence hintText) {
        // If the field is always expanded, then don't add the search icon to the hint
        if (!return hintText;
        SpannableStringBuilder ssb = new SpannableStringBuilder("   "); // for the icon
        ssb.append(hintText);
        Drawable searchIcon = getContext().getResources().getDrawable(getSearchIconId());
        int textSize = (int) (.getTextSize() * 1.25);
        searchIcon.setBounds(0, 0, textSizetextSize);
        ssb.setSpan(new ImageSpan(searchIcon), 1, 2, .);
        return ssb;
    }
    private void updateQueryHint() {
        if ( != null) {
        } else if ( != null) {
            CharSequence hint = null;
            int hintId = .getHintId();
            if (hintId != 0) {
                hint = getContext().getString(hintId);
            }
            if (hint != null) {
                .setHint(getDecoratedHint(hint));
            }
        } else {
            .setHint(getDecoratedHint(""));
        }
    }

    
Updates the auto-complete text view.
    private void updateSearchAutoComplete() {
        // TODO mQueryTextView.setDropDownAnimationStyle(0); // no animation
        int inputType = .getInputType();
        // We only touch this if the input type is set up for text (which it almost certainly
        // should be, in the case of search!)
        if ((inputType & .) == .) {
            // The existence of a suggestions authority is the proxy for "suggestions
            // are available here"
            inputType &= ~.;
            if (.getSuggestAuthority() != null) {
                inputType |= .;
                // TYPE_TEXT_FLAG_AUTO_COMPLETE means that the text editor is performing
                // auto-completion based on its own semantics, which it will present to the user
                // as they type. This generally means that the input method should not show its
                // own candidates, and the spell checker should not be in action. The text editor
                // supplies its candidates by calling InputMethodManager.displayCompletions(),
                // which in turn will call InputMethodSession.displayCompletions().
                inputType |= .;
            }
        }
        .setInputType(inputType);
        if ( != null) {
            .changeCursor(null);
        }
        // attach the suggestions adapter, if suggestions are available
        // The existence of a suggestions authority is the proxy for "suggestions available here"
        if (.getSuggestAuthority() != null) {
             = new SuggestionsAdapter(getContext(),
                    this);
                     ? .
                    : .);
        }
    }

    
Update the visibility of the voice button. There are actually two voice search modes, either of which will activate the button.

Parameters:
empty whether the search query text field is empty. If it is, then the other criteria apply to make the voice button visible.
    private void updateVoiceButton(boolean empty) {
        int visibility = ;
        if ( && !isIconified() && empty) {
            visibility = ;
            .setVisibility();
        }
        .setVisibility(visibility);
    }
    private final OnEditorActionListener mOnEditorActionListener = new OnEditorActionListener() {

        
Called when the input method default action key is pressed.
        public boolean onEditorAction(TextView vint actionIdKeyEvent event) {
            onSubmitQuery();
            return true;
        }
    };
    private void onTextChanged(CharSequence newText) {
        CharSequence text = .getText();
         = text;
        boolean hasText = !TextUtils.isEmpty(text);
        updateSubmitButton(hasText);
        updateVoiceButton(!hasText);
        updateCloseButton();
        updateSubmitArea();
        if ( != null && !TextUtils.equals(newText)) {
            .onQueryTextChange(newText.toString());
        }
         = newText.toString();
    }
    private void onSubmitQuery() {
        CharSequence query = .getText();
        if (query != null && TextUtils.getTrimmedLength(query) > 0) {
            if ( == null
                    || !.onQueryTextSubmit(query.toString())) {
                if ( != null) {
                    launchQuerySearch(.nullquery.toString());
                    setImeVisibility(false);
                }
                dismissSuggestions();
            }
        }
    }
    private void dismissSuggestions() {
    }
    private void onCloseClicked() {
        CharSequence text = .getText();
        if (TextUtils.isEmpty(text)) {
            if () {
                // If the app doesn't override the close behavior
                if ( == null || !.onClose()) {
                    // hide the keyboard and remove focus
                    clearFocus();
                    // collapse the search field
                    updateViewsVisibility(true);
                }
            }
        } else {
            .setText("");
            .requestFocus();
            setImeVisibility(true);
        }
    }
    private void onSearchClicked() {
        updateViewsVisibility(false);
        .requestFocus();
        setImeVisibility(true);
        if ( != null) {
            .onClick(this);
        }
    }
    private void onVoiceClicked() {
        // guard against possible race conditions
        if ( == null) {
            return;
        }
        SearchableInfo searchable = ;
        try {
            if (searchable.getVoiceSearchLaunchWebSearch()) {
                Intent webSearchIntent = createVoiceWebSearchIntent(,
                        searchable);
                getContext().startActivity(webSearchIntent);
            } else if (searchable.getVoiceSearchLaunchRecognizer()) {
                Intent appSearchIntent = createVoiceAppSearchIntent(,
                        searchable);
                getContext().startActivity(appSearchIntent);
            }
        } catch (ActivityNotFoundException e) {
            // Should not happen, since we check the availability of
            // voice search before showing the button. But just in case...
            Log.w("Could not find voice search activity");
        }
    }
    void onTextFocusChanged() {
        // Delayed update to make sure that the focus has settled down and window focus changes
        // don't affect it. A synchronous update was not working.
        postUpdateFocusedState();
        if (.hasFocus()) {
            forceSuggestionQuery();
        }
    }
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        postUpdateFocusedState();
    }

    
    @Override
    public void onActionViewCollapsed() {
        clearFocus();
        updateViewsVisibility(true);
         = false;
    }

    
    @Override
    public void onActionViewExpanded() {
        if (return;
         = true;
        .setText("");
        setIconified(false);
    }
    @Override
        super.onInitializeAccessibilityEvent(event);
        event.setClassName(SearchView.class.getName());
    }
    @Override
        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(SearchView.class.getName());
    }
    private void adjustDropDownSizeAndPosition() {
        if (.getWidth() > 1) {
            Resources res = getContext().getResources();
            int anchorPadding = .getPaddingLeft();
            Rect