SearchView


public class SearchView extends LinearLayoutCompat implements CollapsibleActionView


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, it's collapsed by default, so you must provide an icon for the action.

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

Summary

Nested types

Callbacks for changes to the query text.

Callback interface for selection events on suggestions.

Public constructors

SearchView(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    int defStyleAttr
)

Public methods

void
int
@Attribute(value = "android:imeOptions")
getImeOptions()

Returns the IME options set on the query text field.

int

Returns the input type set on the query text field.

int
@Attribute(value = "android:maxWidth")
getMaxWidth()

Gets the specified maximum width in pixels, if set.

CharSequence

Returns the query string currently in the text field.

@Nullable CharSequence
@Attribute(value = "androidx.appcompat:queryHint")
getQueryHint()

Returns the hint text that will be displayed in the query text field.

CursorAdapter

Returns the adapter used for suggestions, if any.

boolean
@Attribute(value = "androidx.appcompat:iconifiedByDefault")
isIconfiedByDefault()

Returns the default iconified state of the search field.

boolean

Returns the current iconified state of the SearchView.

boolean

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

boolean

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

void

Called when this view is collapsed as an action view.

void

Called when this view is expanded as an action view.

void
onWindowFocusChanged(boolean hasWindowFocus)
boolean
requestFocus(int direction, Rect previouslyFocusedRect)
void
setIconified(boolean iconify)

Iconifies or expands the SearchView.

void
setIconifiedByDefault(boolean iconified)

Sets the default or resting state of the search field.

void
setImeOptions(int imeOptions)

Sets the IME options on the query text field.

void
setInputType(int inputType)

Sets the input type on the query text field.

void
setMaxWidth(int maxpixels)

Makes the view at most this many pixels wide maxWidth

void

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

void

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

void

Sets a listener for user actions within the SearchView.

void

Sets a listener to inform when the search button is pressed.

void

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

void
setQuery(CharSequence query, boolean submit)

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

void

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

void
setQueryRefinementEnabled(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.

void

Sets the SearchableInfo for this SearchView.

void
setSubmitButtonEnabled(boolean enabled)

Enables showing a submit button when the query is non-empty.

void

You can set a custom adapter if you wish.

Protected methods

void
void
onLayout(boolean changed, int left, int top, int right, int bottom)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
void

Called when a query refinement has been proposed, e.g. when the user clicks on a suggestion provided by a SuggestionsAdapter.

void
Parcelable

Inherited Constants

From androidx.appcompat.widget.LinearLayoutCompat
static final int
static final int

Show a divider at the beginning of the group.

static final int

Show a divider at the end of the group.

static final int

Show dividers between each item in the group.

static final int

Don't show any dividers.

static final int
From android.view.View
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final int
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE = "creditCardExpirationDate"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY = "creditCardExpirationDay"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH = "creditCardExpirationMonth"
static final String
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR = "creditCardExpirationYear"
static final String
static final String
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE = "creditCardSecurityCode"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int
static final int
static final int
static final int
static final int
static final int
static final int
GONE = 8
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
KEEP_SCREEN_ON = 67108864
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
NO_ID = -1
static final int
static final int
static final int
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int[]
static final int[]
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
static final String
VIEW_LOG_TAG = "View"
static final int
static final int[]
static final Property<ViewFloat>
static final Property<ViewFloat>
static final Property<ViewFloat>
From android.view.ViewGroup
static final int
static final int
static final int
static final int
static final int
static final int
static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

static final int

This field is deprecated.

Inherited methods

From androidx.appcompat.widget.LinearLayoutCompat
boolean
LinearLayoutCompat.LayoutParams

Returns a set of layout parameters with a width of MATCH_PARENT and a height of WRAP_CONTENT when the layout's orientation is VERTICAL.

LinearLayoutCompat.LayoutParams
LinearLayoutCompat.LayoutParams
int
int
@Attribute(value = "android:baselineAlignedChildIndex")
getBaselineAlignedChildIndex()
Drawable
@Attribute(value = "androidx.appcompat:divider")
getDividerDrawable()
int
@Attribute(value = "androidx.appcompat:dividerPadding")
getDividerPadding()

Get the padding size used to inset dividers in pixels

@GravityInt int
@Attribute(value = "android:gravity")
getGravity()

Returns the current gravity.

int
@Attribute(value = "android:orientation", intMapping = [@Attribute.IntMap(name = "horizontal", value = 0), @Attribute.IntMap(name = "vertical", value = 1)])
getOrientation()

Returns the current orientation.

int
@Attribute(value = "androidx.appcompat:showDividers", intMapping = [@Attribute.IntMap(name = "none", value = 0), @Attribute.IntMap(name = "beginning", value = 1, mask = 1), @Attribute.IntMap(name = "middle", value = 2, mask = 2), @Attribute.IntMap(name = "end", value = 4, mask = 4)])
getShowDividers()
float
@Attribute(value = "android:weightSum")
getWeightSum()

Returns the desired weights sum.

boolean
@Attribute(value = "android:baselineAligned")
isBaselineAligned()

Indicates whether widgets contained within this layout are aligned on their baseline or not.

boolean
@Attribute(value = "androidx.appcompat:measureWithLargestChild")
isMeasureWithLargestChildEnabled()

When true, all children with a weight will be considered having the minimum size of the largest child.

void
void
void
void
setBaselineAligned(boolean baselineAligned)

Defines whether widgets contained in this layout are baseline-aligned or not.

void
void

Set a drawable to be used as a divider between items.

void
setDividerPadding(int padding)

Set padding displayed on both ends of dividers.

void
setGravity(@GravityInt int gravity)

Describes how the child views are positioned.

void
setHorizontalGravity(int horizontalGravity)
void

When set to true, all children with a weight will be considered having the minimum size of the largest child.

void
setOrientation(int orientation)

Should the layout be a column or a row.

void
setShowDividers(int showDividers)

Set how dividers should be shown between items in this layout

void
setVerticalGravity(int verticalGravity)
void
setWeightSum(float weightSum)

Defines the desired weights sum.

boolean
From android.view.View
void
void
void
ViewPropertyAnimator
void
void
boolean
void
void

This method is deprecated.

void
boolean
boolean
boolean
boolean
boolean
canScrollHorizontally(int direction)
boolean
canScrollVertically(int direction)
final void
void
final void
boolean
void
void
static int
combineMeasuredStates(int curState, int newState)
int
int
int
void
WindowInsets
int
int
int
AccessibilityNodeInfo
void
void

This method is deprecated.

boolean
boolean
dispatchNestedFling(float velocityX, float velocityY, boolean consumed)
boolean
dispatchNestedPreFling(float velocityX, float velocityY)
boolean
dispatchNestedPrePerformAccessibilityAction(
    int action,
    Bundle arguments
)
boolean
dispatchNestedPreScroll(
    int dx,
    int dy,
    int[] consumed,
    int[] offsetInWindow
)
boolean
dispatchNestedScroll(
    int dxConsumed,
    int dyConsumed,
    int dxUnconsumed,
    int dyUnconsumed,
    int[] offsetInWindow
)
boolean
void
draw(Canvas canvas)
void
drawableHotspotChanged(float x, float y)
final OnBackInvokedDispatcher
final T
<T extends View> findViewById(int id)
final T
<T extends View> findViewWithTag(Object tag)
boolean

This method is deprecated.

void
forceHasOverlappingRendering(boolean hasOverlappingRendering)
void
void
generateDisplayHash(
    String hashAlgorithm,
    Rect bounds,
    Executor executor,
    DisplayHashResultCallback callback
)
static int
View.AccessibilityDelegate
int
AccessibilityNodeProvider
CharSequence
int
int
String
String
float
Animation
Matrix
IBinder
int[]
Map<IntegerInteger>
String[]
final AutofillId
int
AutofillValue
Drawable
BlendMode
ColorStateList
PorterDuff.Mode
int
final int
float
int
float
Rect
boolean
final boolean
final ContentCaptureSession
CharSequence
final Context
ContextMenu.ContextMenuInfo
final boolean
static int
getDefaultSize(int size, int measureSpec)
Display
final int[]
Bitmap

This method is deprecated.

int

This method is deprecated.

int

This method is deprecated.

void