Trending searches
Stay organized with collections
Save and categorize content based on your preferences.
Request trending searches UI view generator
Similar to request search suggestions, to get the trending searches UI view, you
should get its generator first by the following steps:
- Let your target
Activity
class implement the
GetSearchSuggestionsViewGeneratorCallback
interface or use the anonymous
inner class.
- Override the
GetSearchSuggestionsViewGeneratorCallback
interface's
onSuccess(SearchSuggestionsViewGenerator)
and onError(String)
methods.
- Construct the
GetTrendingSearchesViewOptions
class instance with the
maximum number of trending searches specified. (Optional) This object also
takes a SearchSuggestionsViewOptions
object which provides some options to
customize the appearance of the search suggestions UI.
- Call
SearchInAppsService
's
getTrendingSearchesView(GetTrendingSearchesViewOptions,
GetSearchSuggestionsViewGeneratorCallback)
function.
- After you get the UI generator, you can consider storing it in a ViewModel
so that you don't need to ask for the generator again when the activity
needs to to be recreated (like when the configuration has changed while the
app is running).
Sample code
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;
import com.google.android.libraries.searchinapps.GetTrendingSearchesViewOptions;
import com.google.android.libraries.searchinapps.SearchInAppsService;
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
...
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
// Uses the default SearchSuggestionsViewOptions.
service.getTrendingSearchesView(
new GetTrendingSearchesViewOptions().setMaxNumTrends(3), this);
...
}
@Override
public void onDestroy() {
service.shutDown();
super.onDestroy();
}
}
Jetpack Compose
package ...
...
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback
import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions
import com.google.android.libraries.searchinapps.SearchInAppsService
import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator
...
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SearchSuggestionsUI()
}
}
@Composable
fun SearchSuggestionsUI() {
...
var service by remember {
mutableStateOf<SearchInAppsService?>(
SearchInAppsService.create(LocalContext.current))
}
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
...
}
override fun onError(errorMessage: String) {
...
}
}
// Uses the default SearchSuggestionsViewOptions.
var options: GetTrendingSearchesViewOptions =
GetTrendingSearchesViewOptions()
.setMaxNumTrends(3)
service?.getTrendingSearchesView(options, callback)
...
}
}
Add trending searches UI view
The way to add trending searches UI view into your app UI is the same as the
search suggestions
feature.
Next: Display search resultsarrow_forward
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-11-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-21 UTC."],[[["\u003cp\u003eTo display trending searches in your Android app, you need to obtain a UI view generator from the \u003ccode\u003eSearchInAppsService\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eImplement the \u003ccode\u003eGetSearchSuggestionsViewGeneratorCallback\u003c/code\u003e interface in your Activity to handle the success or failure of the generator retrieval.\u003c/p\u003e\n"],["\u003cp\u003eCustomize the number of trending searches displayed and the UI appearance using \u003ccode\u003eGetTrendingSearchesViewOptions\u003c/code\u003e and \u003ccode\u003eSearchSuggestionsViewOptions\u003c/code\u003e respectively.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the obtained generator to seamlessly integrate the trending searches UI into your app's layout, similar to adding search suggestions.\u003c/p\u003e\n"],["\u003cp\u003eStore the generator in a ViewModel to avoid redundant requests when the activity is recreated.\u003c/p\u003e\n"]]],["To implement trending searches, first, have your `Activity` implement `GetSearchSuggestionsViewGeneratorCallback`. Override `onSuccess` and `onError` methods. Optionally, create a `GetTrendingSearchesViewOptions` instance to customize settings like the maximum number of trends. Call `SearchInAppsService`'s `getTrendingSearchesView`, passing the options and callback. Store the retrieved `SearchSuggestionsViewGenerator` in a ViewModel to avoid repeated requests. Adding the generated view into your app's UI is done similarly to search suggestions.\n"],null,["# Trending searches\n\nRequest trending searches UI view generator\n-------------------------------------------\n\nSimilar to request search suggestions, to get the trending searches UI view, you\nshould get its generator first by the following steps:\n\n1. Let your target `Activity` class implement the `GetSearchSuggestionsViewGeneratorCallback` interface or use the anonymous inner class.\n2. Override the `GetSearchSuggestionsViewGeneratorCallback` interface's `onSuccess(SearchSuggestionsViewGenerator)` and `onError(String)` methods.\n3. Construct the `GetTrendingSearchesViewOptions` class instance with the maximum number of trending searches specified. (Optional) This object also takes a `SearchSuggestionsViewOptions` object which provides some options to customize the appearance of the search suggestions UI.\n4. Call `SearchInAppsService`'s `getTrendingSearchesView(GetTrendingSearchesViewOptions,\n GetSearchSuggestionsViewGeneratorCallback)` function.\n5. After you get the UI generator, you can consider storing it in a ViewModel so that you don't need to ask for the generator again when the activity needs to to be recreated (like when the configuration has changed while the app is running).\n\nSample code \n\n### Java\n\n package ...;\n\n ...\n import androidx.appcompat.app.AppCompatActivity;\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback;\n import com.google.android.libraries.searchinapps.GetTrendingSearchesViewOptions;\n import com.google.android.libraries.searchinapps.SearchInAppsService;\n import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator;\n ...\n\n public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {\n private SearchInAppsService service;\n\n @Override\n public void onSuccess(SearchSuggestionsViewGenerator generator) {\n ...\n }\n\n @Override\n public void onError(String errorMessage) {\n ...\n }\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n ...\n service = SearchInAppsService.create(this);\n // Uses the default SearchSuggestionsViewOptions.\n service.getTrendingSearchesView(\n new GetTrendingSearchesViewOptions().setMaxNumTrends(3), this);\n ...\n }\n\n @Override\n public void onDestroy() {\n service.shutDown();\n super.onDestroy();\n }\n }\n\n### Jetpack Compose\n\n package ...\n\n ...\n import android.os.Bundle\n import androidx.activity.compose.setContent\n import androidx.appcompat.app.AppCompatActivity\n import androidx.compose.runtime.Composable\n import androidx.compose.runtime.DisposableEffect\n import androidx.compose.runtime.mutableStateOf\n import androidx.compose.ui.platform.LocalContext\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewGeneratorCallback\n import com.google.android.libraries.searchinapps.GetSearchSuggestionsViewOptions\n import com.google.android.libraries.searchinapps.SearchInAppsService\n import com.google.android.libraries.searchinapps.SearchSuggestionsViewGenerator\n ...\n\n class MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContent {\n SearchSuggestionsUI()\n }\n }\n\n @Composable\n fun SearchSuggestionsUI() {\n ...\n var service by remember {\n mutableStateOf\u003cSearchInAppsService?\u003e(\n SearchInAppsService.create(LocalContext.current))\n }\n DisposableEffect(Unit) { onDispose { service?.shutDown() } }\n val callback =\n object : GetSearchSuggestionsViewGeneratorCallback {\n override fun onSuccess(generator: SearchSuggestionsViewGenerator) {\n ...\n }\n\n override fun onError(errorMessage: String) {\n ...\n }\n }\n // Uses the default SearchSuggestionsViewOptions.\n var options: GetTrendingSearchesViewOptions =\n GetTrendingSearchesViewOptions()\n .setMaxNumTrends(3)\n service?.getTrendingSearchesView(options, callback)\n ...\n }\n }\n\nAdd trending searches UI view\n-----------------------------\n\nThe way to add trending searches UI view into your app UI is the same as the\n[search suggestions](/search-in-apps/android/search_suggestions#add_search_suggestions_ui_view)\nfeature.\n\n[Next: Display search resultsarrow_forward](/search-in-apps/android/display_search_results)"]]