Discover Search Widget | HMS Site Kit

Mustafa Sürücü
Huawei Developers
Published in
3 min readSep 15, 2020

Most of the developers use the services of searching places, displaying or utilizing place-related information in mobile applications. You need two basic requirements to provide a complete searchbar for your users. The first thing is a layout which enables to use searching activities like entering a keyword or showing suggestions according to user’s input, and the second thing is an API which enables to utilize all place-related information to the users.

HMS Site Kit offers you a Widget that meets all your needs to provide place search services and fetching their information. It is a search component of the built-in place search suggestion feature. When a users enters a keyword in the search box, the widget displays the list of suggested places for user. If the user clicks on any place in the list, your app will receive a Site object and use it to obtain place details.

If you have any questions about how you can integrate HMS Core into your project, please take a look at below post before beginning.

Note: Please do not forget to activate Site Kit in AppGalery Connect (Project Setting → Manage API) and to add build dependencies to app level build.gradle file (implementation ‘com.huawei.hms:site:{version}’).

Let’s see how to use Widget of the HMS Site Kit with an example. You can use widget by adding a SearchFragment object to your layout or using the Intent object. We will use it by adding fragment in our demo project.

You can see the fragment and a cardview in the layout given above. When users selects a place from the widget, we will fetch some information about that place and display in that card. The next step is to prepare our interface class.

Two interfaces are defined one for presenter and the another is for view. initWidget() will be called with presenter object to initialize the Widget component and to set SiteSelectedListener. The view instance will get the user inputs and show place details with getMyFragment() and showMySite() methods.

In WidgetActivity class, searchFragment is defined and initWidget method has been called with presenter object. Moreover, override methods of view interface are also defined here.

In WidgetPresenter class, initWidget() method has been generated. The searchFragment is obtained via view instance and API Key has been set. Then, SiteSelectedListener has been set from the fragment to determine user selection. When user clicks on a place, details of place are obtained with showMySite() method by using the site object that returns from the API.

Note: “API Key” is required to use Widget. You can find it in agconnect-services.json file.

Widget
User Selection

This project has been developed with an architecture that enables to provide maintainable and adaptable design for developers. You can build up your app based on it to shape your usage.

In this article, we discovered the usage of Widget Component that offerred by HMS Site Kit. I hope that it will provide greate convenience for your projects.

Thank you for reading !

References

--

--