Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet146/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   142   143   144   145   146   147   148   149   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

169
CHAPTER 8: APIs
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass=
"android.support.v7.widget.SearchView"
android:icon=
"@android:drawable/ic_menu_search"/>

What then needs to be done inside your app to connect the widget with the search 
framework is the following:
// Set the searchable configuration
val searchManager = getSystemService(SEARCH_SERVICE)
as SearchManager
val searchView = menu.findItem(R.id.action_search).
actionView as SearchView
searchView.setSearchableInfo(
searchManager.getSearchableInfo(componentName))
// Do not iconify the widget; expand it by default:
searchView.setIconifiedByDefault(false)
That is it! The flow looks like Figure 
8-8
(you’d click the search icon in the action bar).
Figure 8-8. A widget-based search flow


170
CHAPTER 8: APIs
Search Suggestions
There are two ways you can help the user input search query strings. You can let the system 
memorize queries for the next time the search gets used, and you can let your app provide 
fully customizable suggestions.
Recent Queries Suggestions
For the recent queries suggestions, you implement a content provider subclass of 
SearchRecentSuggestionsProvider and add it to AndroidManifest.xml like any other content 
provider. A basic but nevertheless already fully implemented content provider looks like this:
class RecentsProvider :
SearchRecentSuggestionsProvider {
val AUTHORITY = "com.example.RecentsProvider"
val MODE = DATABASE_MODE_QUERIES
init {
setupSuggestions(AUTHORITY, MODE)
}
}
Register it inside /res/xml/searchable.xml as follows:

"http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority=
"com.example.RecentsProvider"
android:searchSuggestSelection=
" ?">

New are the last two attributes. The android:searchSuggestAuthority attribute here draws a 
connection to the provider.
The content provider must still be registered in AndroidManifest.xml. This for example reads 
as follows:
android:name=".RecentsProvider"
android:authorities="com.example.RecentsProvider"
android:enabled="true"
android:exported="true">
This reads previous queries from an automatically generated database. What is left to do is 
add the search queries. For this aim, inside the SearchableActivity class write the following:



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   142   143   144   145   146   147   148   149   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling