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 theGetSearchSuggestionsViewGeneratorCallback
interface or use the anonymous inner class. - Override the
GetSearchSuggestionsViewGeneratorCallback
interface'sonSuccess(SearchSuggestionsViewGenerator)
andonError(String)
methods. - Construct the
GetTrendingSearchesViewOptions
class instance with the maximum number of trending searches specified. (Optional) This object also takes aSearchSuggestionsViewOptions
object which provides some options to customize the appearance of the search suggestions UI. - Call
SearchInAppsService
'sgetTrendingSearchesView(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.