Android 12 Developer Preview is here! Try it out, and give us your feedback!

SearchTemplate.Builder

public static final class SearchTemplate.Builder
extends Object

java.lang.Object
   ↳ com.google.android.libraries.car.app.model.SearchTemplate.Builder


A builder of SearchTemplate.

Summary

Public methods

SearchTemplate build()

Constructs the SearchTemplate model.

SearchTemplate.Builder setActionStrip(ActionStrip actionStrip)

Sets the ActionStrip for this template, or null to not display an ActionStrip.

SearchTemplate.Builder setHeaderAction(Action headerAction)

Sets the Action that will be displayed in the header of the template, or null to not display an action.

SearchTemplate.Builder setInitialSearchText(String initialSearchText)

Sets the initial search text to display in the search box, or null to not display any initial search text.

SearchTemplate.Builder setIsLoading(boolean isLoading)

Sets whether the template is in a loading state.

SearchTemplate.Builder setItemList(ItemList itemList)

Sets the ItemList to show for search results, or null if there are no results.

SearchTemplate.Builder setSearchHint(String searchHint)

Sets the text hint to display in the search box when it is empty, or null to use a default search hint.

SearchTemplate.Builder setShowKeyboardByDefault(boolean showKeyboardByDefault)

Sets if the keyboard should be displayed by default, instead of waiting until user interacts with the search box.

Inherited methods

Public methods

build

public SearchTemplate build ()

Constructs the SearchTemplate model.

Returns
SearchTemplate

Throws
IllegalArgumentException if the template is in a loading state but the list is set.

setActionStrip

public SearchTemplate.Builder setActionStrip (ActionStrip actionStrip)

Sets the ActionStrip for this template, or null to not display an ActionStrip.

Requirements

This template allows up to 2 Actions in its ActionStrip. Of the 2 allowed Actions, one of them can contain a title as set via Action.Builder.setTitle(CharSequence). Otherwise, only Actions with icons are allowed.

Parameters
actionStrip ActionStrip

Returns
SearchTemplate.Builder

Throws
IllegalArgumentException if actionStrip does not meet the template's requirements.

setHeaderAction

public SearchTemplate.Builder setHeaderAction (Action headerAction)

Sets the Action that will be displayed in the header of the template, or null to not display an action.

Requirements

This template only supports either either one of Action.APP_ICON and Action.BACK as a header Action.

Parameters
headerAction Action

Returns
SearchTemplate.Builder

Throws
IllegalArgumentException if headerAction does not meet the template's requirements.

setInitialSearchText

public SearchTemplate.Builder setInitialSearchText (String initialSearchText)

Sets the initial search text to display in the search box, or null to not display any initial search text.

Defaults to null.

Parameters
initialSearchText String

Returns
SearchTemplate.Builder

setIsLoading

public SearchTemplate.Builder setIsLoading (boolean isLoading)

Sets whether the template is in a loading state.

If set to true, the UI will display a loading indicator where the list content would be otherwise. The caller is expected to call Screen.invalidate() and send the new template content to the host once the data is ready. If set to false, the UI shows the ItemList contents added via setItemList(ItemList).

Parameters
isLoading boolean

Returns
SearchTemplate.Builder

setItemList

public SearchTemplate.Builder setItemList (ItemList itemList)

Sets the ItemList to show for search results, or null if there are no results.

The list will be shown below the search box, allowing users to click on individual search results.

Requirements

This template allows up to 6 Rows in the ItemList. The host will ignore any items over that limit. The list itself cannot be selectable as set via ItemList.Builder.setSelectable(ItemList.OnSelectedListener). Each Row can add up to 2 lines of texts via Row.Builder.addText(CharSequence) and cannot contain a Toggle.

Parameters
itemList ItemList

Returns
SearchTemplate.Builder

Throws
IllegalArgumentException if itemList does not meet the template's requirements.

setSearchHint

public SearchTemplate.Builder setSearchHint (String searchHint)

Sets the text hint to display in the search box when it is empty, or null to use a default search hint.

This is not the actual search text, and will disappear if user types any value into the search.

If a non empty text is set via setInitialSearchText(String), the searchHint will not show, unless the user erases the search text.

Defaults to null.

Parameters
searchHint String

Returns
SearchTemplate.Builder

setShowKeyboardByDefault

public SearchTemplate.Builder setShowKeyboardByDefault (boolean showKeyboardByDefault)

Sets if the keyboard should be displayed by default, instead of waiting until user interacts with the search box.

Defaults to true.

Parameters
showKeyboardByDefault boolean

Returns
SearchTemplate.Builder