Right now one client ID can only support one search feature. If you want to use multiple search features in your app, you should use multiple client IDs. In addition, if you want to use the same search feature in different entry points in your app, you should also use different client IDs for each of them to distinguish the traffic.
For your each request, you can proactively set a client ID in
GetSearchSuggestionsViewOptions
, GetTrendingSearchesViewOptions
or
GetSearchContentViewOptions
, and this ID will override the default client ID
in the AndroidManifest.xml
file (only for this request).
Sample code
Java
package ...;
...
import androidx.appcompat.app.AppCompatActivity;
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;
import java.util.Arrays;
import java.util.List;
...
public class MainActivity extends AppCompatActivity implements GetSearchSuggestionsViewGeneratorCallback {
private SearchInAppsService service;
@Override
public void onSuccess(SearchSuggestionsViewGenerator generator) {
ViewGroup container = findViewById(R.id.[container_id]);
container.removeAllViews();
container.addView(generator.populateView(this));
}
@Override
public void onError(String errorMessage) {
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
service = SearchInAppsService.create(this);
List<String> searchContext = Arrays.asList(new String[]{"Query"});
service.getSearchSuggestionsView(
new GetSearchSuggestionsViewOptions().setTextContext(searchContext)
.setClientId("client id"),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.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
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))
}
var viewGenerator = mutableStateOf<SearchSuggestionsViewGenerator?>(null)
DisposableEffect(Unit) { onDispose { service?.shutDown() } }
val callback =
object : GetSearchSuggestionsViewGeneratorCallback {
override fun onSuccess(generator: SearchSuggestionsViewGenerator) {
viewGenerator.value = generator
}
override fun onError(errorMessage: String) {}
}
var searchContexts: List<String> = listOf<String>("Query")
var options: GetSearchSuggestionsViewOptions =
GetSearchSuggestionsViewOptions().setTextContext(searchContexts)
.setClientId("client id")
service?.getSearchSuggestionsView(options, callback)
ChipGroupUI(viewGenerator)
...
}
@Composable
fun ChipGroupUI(viewGenerator: MutableState<SearchSuggestionsViewGenerator?>) {
viewGenerator.value?.let { viewGenerator ->
var context = LocalContext.current
AndroidView(
factory = { context -> viewGenerator.populateView(context) },
update = { view -> viewGenerator.updateView(view, context) },
)
}
}
}