Pro Android with Kotlin


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

164
CHAPTER 8: APIs
To generate such a quick contact badge, inside your layout file you must add the following:
android:id="@+id/quickBadge"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"/>
In you code you must connect the badge to the following information you get from 
the contacts provider: the raw contact ID, the lookup key, and a thumbnail URI. The 
corresponding code might look like this:
val id = row[ContactsContract.Contacts._ID]
val lookup = row[ContactsContract.Contacts.
LOOKUP_KEY]
val photo = row[ContactsContract.Contacts.
PHOTO_THUMBNAIL_URI]
Here, the row, for example, is a map you get from a contacts content provider query. In this 
case, a query in Raw Contact is enough; you don’t need to also query the Data table.
Figure 8-6. A quick contact badge


165
CHAPTER 8: APIs
From here we configure the badge as follows, for example after you load the contact 
information by user interface activities:
val contactUri = ContactsContract.Contacts.getLookupUri(
id?.toLong()?:0,
lookup)
quickBadge.assignContactUri(contactUri)
val thumbnail =
loadContactPhotoThumbnail(photo.toString())
quickBadge.setImageBitmap(thumbnail)
Here, the loadContactPhotoThumbnail() function loads the thumbnail image data.
private fun loadContactPhotoThumbnail(photoData: String):
Bitmap? {
var afd: AssetFileDescriptor? = null
try {
val thumbUri = Uri.parse(photoData)
afd = contentResolver.
openAssetFileDescriptor(thumbUri, "r")
afd?.apply {
fileDescriptor?.apply {
return BitmapFactory.decodeFileDescriptor(
this, null, null)
}
}
} catch (e: FileNotFoundException) {
// Handle file not found errors ...
} finally {
afd?.close()
}
return null
}
Search Framework
The search framework allows you to seamlessly add search functionality to your app and 
register your app as a searchable items provider in the Android OS.
Talking of the user interface, you have two options.

Opening a search dialog

Adding a search widget to your UI via SearchView



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   139   140   141   142   143   144   145   146   ...   223




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