0
# Content Sharing
1
2
The Facebook Share module provides comprehensive content sharing capabilities including links, photos, videos, and Open Graph stories. It supports both programmatic sharing via ShareApi and UI-based sharing through various dialog implementations.
3
4
## Share API
5
6
Programmatic sharing without user interaction:
7
8
```kotlin { .api }
9
class ShareApi(
10
content: ShareContent<*, *>,
11
mode: ShareApi.Mode
12
) {
13
enum class Mode {
14
AUTOMATIC, // Choose best available method
15
NATIVE, // Use native Facebook app
16
WEB // Use web-based sharing
17
}
18
19
fun share(callback: FacebookCallback<Sharer.Result>?)
20
fun canShare(): Boolean
21
22
companion object {
23
fun share(
24
activity: Activity,
25
shareContent: ShareContent<*, *>,
26
callback: FacebookCallback<Sharer.Result>?
27
)
28
}
29
}
30
31
interface Sharer {
32
data class Result(val postId: String?)
33
}
34
```
35
36
### Share API Usage
37
38
```kotlin
39
// Create share content
40
val linkContent = ShareLinkContent.Builder()
41
.setContentUrl(Uri.parse("https://example.com"))
42
.setQuote("Check out this amazing content!")
43
.build()
44
45
// Share programmatically
46
val shareApi = ShareApi(linkContent, ShareApi.Mode.AUTOMATIC)
47
if (shareApi.canShare()) {
48
shareApi.share(object : FacebookCallback<Sharer.Result> {
49
override fun onSuccess(result: Sharer.Result) {
50
Log.d("Share", "Shared successfully, post ID: ${result.postId}")
51
}
52
53
override fun onCancel() {
54
Log.d("Share", "Share cancelled")
55
}
56
57
override fun onError(error: FacebookException) {
58
Log.e("Share", "Share error: ${error.message}")
59
}
60
})
61
} else {
62
Log.w("Share", "Cannot share this content type")
63
}
64
```
65
66
## Share Dialogs
67
68
UI-based sharing dialogs that provide a rich sharing experience:
69
70
```kotlin { .api }
71
class ShareDialog : FacebookDialogBase<ShareContent<*, *>, ShareDialog.Result> {
72
constructor(activity: Activity)
73
constructor(fragment: Fragment)
74
constructor(fragment: android.app.Fragment)
75
76
companion object {
77
fun canShow(contentType: Class<out ShareContent<*, *>>): Boolean
78
}
79
80
fun show(content: ShareContent<*, *>)
81
fun registerCallback(
82
callbackManager: CallbackManager,
83
callback: FacebookCallback<Result>
84
)
85
86
data class Result(val postId: String?)
87
}
88
89
class MessageDialog : FacebookDialogBase<ShareContent<*, *>, MessageDialog.Result> {
90
constructor(activity: Activity)
91
constructor(fragment: Fragment)
92
93
companion object {
94
fun canShow(contentType: Class<out ShareContent<*, *>>): Boolean
95
}
96
97
fun show(content: ShareContent<*, *>)
98
99
data class Result(val requestId: String?)
100
}
101
```
102
103
### Share Dialog Usage
104
105
```kotlin
106
class SharingActivity : AppCompatActivity() {
107
private lateinit var callbackManager: CallbackManager
108
private lateinit var shareDialog: ShareDialog
109
110
override fun onCreate(savedInstanceState: Bundle?) {
111
super.onCreate(savedInstanceState)
112
113
callbackManager = CallbackManager.Factory.create()
114
shareDialog = ShareDialog(this)
115
116
shareDialog.registerCallback(callbackManager, object : FacebookCallback<ShareDialog.Result> {
117
override fun onSuccess(result: ShareDialog.Result) {
118
Log.d("Share", "Content shared successfully")
119
}
120
121
override fun onCancel() {
122
Log.d("Share", "Share cancelled")
123
}
124
125
override fun onError(error: FacebookException) {
126
Log.e("Share", "Share error: ${error.message}")
127
}
128
})
129
130
// Share a link
131
shareLink()
132
}
133
134
private fun shareLink() {
135
val content = ShareLinkContent.Builder()
136
.setContentUrl(Uri.parse("https://developers.facebook.com"))
137
.setQuote("Learn how to integrate Facebook into your app!")
138
.build()
139
140
if (ShareDialog.canShow(ShareLinkContent::class.java)) {
141
shareDialog.show(content)
142
}
143
}
144
145
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
146
callbackManager.onActivityResult(requestCode, resultCode, data)
147
super.onActivityResult(requestCode, resultCode, data)
148
}
149
}
150
```
151
152
## Share Content Types
153
154
### Base Share Content
155
156
```kotlin { .api }
157
abstract class ShareContent<P : ShareContent<P, E>, E : ShareContent.Builder<P, E>> {
158
val contentUrl: Uri?
159
val peopleIds: List<String>
160
val placeId: String?
161
val ref: String?
162
val shareHashtag: ShareHashtag?
163
164
abstract class Builder<P : ShareContent<P, E>, E : Builder<P, E>> {
165
fun setContentUrl(contentUrl: Uri?): E
166
fun setPeopleIds(peopleIds: List<String>?): E
167
fun setPlaceId(placeId: String?): E
168
fun setRef(ref: String?): E
169
fun setShareHashtag(shareHashtag: ShareHashtag?): E
170
abstract fun build(): P
171
}
172
}
173
174
data class ShareHashtag(val hashtag: String) {
175
class Builder {
176
fun setHashtag(hashtag: String?): Builder
177
fun build(): ShareHashtag
178
}
179
}
180
```
181
182
### Link Content
183
184
```kotlin { .api }
185
class ShareLinkContent : ShareContent<ShareLinkContent, ShareLinkContent.Builder> {
186
val quote: String?
187
188
class Builder : ShareContent.Builder<ShareLinkContent, Builder>() {
189
fun setQuote(quote: String?): Builder
190
override fun build(): ShareLinkContent
191
}
192
}
193
```
194
195
### Photo Content
196
197
```kotlin { .api }
198
class SharePhotoContent : ShareContent<SharePhotoContent, SharePhotoContent.Builder> {
199
val photos: List<SharePhoto>
200
201
class Builder : ShareContent.Builder<SharePhotoContent, Builder>() {
202
fun setPhotos(photos: List<SharePhoto>?): Builder
203
fun addPhoto(photo: SharePhoto?): Builder
204
override fun build(): SharePhotoContent
205
}
206
}
207
208
class SharePhoto {
209
val bitmap: Bitmap?
210
val imageUrl: Uri?
211
val caption: String?
212
val userGenerated: Boolean
213
214
class Builder {
215
fun setBitmap(bitmap: Bitmap?): Builder
216
fun setImageUrl(imageUrl: Uri?): Builder
217
fun setCaption(caption: String?): Builder
218
fun setUserGenerated(userGenerated: Boolean): Builder
219
fun build(): SharePhoto
220
}
221
}
222
```
223
224
### Video Content
225
226
```kotlin { .api }
227
class ShareVideoContent : ShareContent<ShareVideoContent, ShareVideoContent.Builder> {
228
val video: ShareVideo
229
val previewPhoto: SharePhoto?
230
231
class Builder : ShareContent.Builder<ShareVideoContent, Builder>() {
232
fun setVideo(video: ShareVideo?): Builder
233
fun setPreviewPhoto(previewPhoto: SharePhoto?): Builder
234
override fun build(): ShareVideoContent
235
}
236
}
237
238
class ShareVideo {
239
val localUrl: Uri?
240
241
class Builder {
242
fun setLocalUrl(localUrl: Uri?): Builder
243
fun build(): ShareVideo
244
}
245
}
246
```
247
248
### Media Content
249
250
```kotlin { .api }
251
class ShareMediaContent : ShareContent<ShareMediaContent, ShareMediaContent.Builder> {
252
val media: List<ShareMedia>
253
254
class Builder : ShareContent.Builder<ShareMediaContent, Builder>() {
255
fun setMedia(media: List<ShareMedia>?): Builder
256
fun addMedium(medium: ShareMedia?): Builder
257
override fun build(): ShareMediaContent
258
}
259
}
260
261
interface ShareMedia
262
```
263
264
### Photo Sharing Example
265
266
```kotlin
267
fun sharePhotos(bitmaps: List<Bitmap>) {
268
val photos = bitmaps.map { bitmap ->
269
SharePhoto.Builder()
270
.setBitmap(bitmap)
271
.setCaption("Shared from my app")
272
.setUserGenerated(true)
273
.build()
274
}
275
276
val content = SharePhotoContent.Builder()
277
.setPhotos(photos)
278
.setShareHashtag(ShareHashtag.Builder().setHashtag("#MyApp").build())
279
.build()
280
281
if (ShareDialog.canShow(SharePhotoContent::class.java)) {
282
shareDialog.show(content)
283
}
284
}
285
```
286
287
### Video Sharing Example
288
289
```kotlin
290
fun shareVideo(videoUri: Uri, previewBitmap: Bitmap?) {
291
val video = ShareVideo.Builder()
292
.setLocalUrl(videoUri)
293
.build()
294
295
val contentBuilder = ShareVideoContent.Builder()
296
.setVideo(video)
297
298
if (previewBitmap != null) {
299
val previewPhoto = SharePhoto.Builder()
300
.setBitmap(previewBitmap)
301
.build()
302
contentBuilder.setPreviewPhoto(previewPhoto)
303
}
304
305
val content = contentBuilder.build()
306
307
if (ShareDialog.canShow(ShareVideoContent::class.java)) {
308
shareDialog.show(content)
309
}
310
}
311
```
312
313
## Share Buttons
314
315
UI components for easy sharing integration:
316
317
```kotlin { .api }
318
class ShareButton : ShareButtonBase {
319
constructor(context: Context)
320
constructor(context: Context, attrs: AttributeSet?)
321
322
var shareContent: ShareContent<*, *>?
323
324
fun registerCallback(
325
callbackManager: CallbackManager,
326
callback: FacebookCallback<Sharer.Result>
327
)
328
}
329
330
class SendButton : ShareButtonBase {
331
constructor(context: Context)
332
constructor(context: Context, attrs: AttributeSet?)
333
334
var shareContent: ShareContent<*, *>?
335
336
fun registerCallback(
337
callbackManager: CallbackManager,
338
callback: FacebookCallback<Sharer.Result>
339
)
340
}
341
342
abstract class ShareButtonBase : FacebookButtonBase {
343
protected val requestCode: Int
344
345
protected abstract fun getDialog(): FacebookDialogBase<*, *>?
346
}
347
```
348
349
### Share Button Usage
350
351
```kotlin
352
// In your layout: <com.facebook.share.widget.ShareButton ... />
353
val shareButton: ShareButton = findViewById(R.id.share_button)
354
355
// Set content to share
356
val linkContent = ShareLinkContent.Builder()
357
.setContentUrl(Uri.parse("https://example.com"))
358
.setQuote("Check this out!")
359
.build()
360
361
shareButton.shareContent = linkContent
362
363
// Register callback
364
shareButton.registerCallback(callbackManager, object : FacebookCallback<Sharer.Result> {
365
override fun onSuccess(result: Sharer.Result) {
366
Log.d("Share", "Shared successfully")
367
}
368
369
override fun onCancel() {
370
Log.d("Share", "Share cancelled")
371
}
372
373
override fun onError(error: FacebookException) {
374
Log.e("Share", "Share error: ${error.message}")
375
}
376
})
377
```
378
379
## Game Requests
380
381
Send game requests to friends:
382
383
```kotlin { .api }
384
class GameRequestDialog : FacebookDialogBase<GameRequestContent, GameRequestDialog.Result> {
385
constructor(activity: Activity)
386
constructor(fragment: Fragment)
387
388
companion object {
389
fun canShow(): Boolean
390
}
391
392
fun show(content: GameRequestContent)
393
394
data class Result(
395
val requestId: String,
396
val to: List<String>
397
)
398
}
399
400
class GameRequestContent : ShareContent<GameRequestContent, GameRequestContent.Builder> {
401
val message: String
402
val recipients: List<String>?
403
val title: String?
404
val data: String?
405
val actionType: ActionType?
406
val objectId: String?
407
val filters: Filters?
408
val suggestions: List<String>?
409
410
enum class ActionType {
411
SEND, ASKFOR, TURN
412
}
413
414
enum class Filters {
415
APP_USERS, APP_NON_USERS
416
}
417
418
class Builder : ShareContent.Builder<GameRequestContent, Builder>() {
419
fun setMessage(message: String): Builder
420
fun setRecipients(recipients: List<String>?): Builder
421
fun setTitle(title: String?): Builder
422
fun setData(data: String?): Builder
423
fun setActionType(actionType: ActionType?): Builder
424
fun setObjectId(objectId: String?): Builder
425
fun setFilters(filters: Filters?): Builder
426
fun setSuggestions(suggestions: List<String>?): Builder
427
override fun build(): GameRequestContent
428
}
429
}
430
```
431
432
### Game Request Example
433
434
```kotlin
435
fun sendGameRequest() {
436
val content = GameRequestContent.Builder()
437
.setMessage("Come play with me!")
438
.setTitle("Join My Game")
439
.setActionType(GameRequestContent.ActionType.SEND)
440
.setFilters(GameRequestContent.Filters.APP_NON_USERS)
441
.build()
442
443
val dialog = GameRequestDialog(this)
444
dialog.registerCallback(callbackManager, object : FacebookCallback<GameRequestDialog.Result> {
445
override fun onSuccess(result: GameRequestDialog.Result) {
446
Log.d("GameRequest", "Request sent to ${result.to.size} friends")
447
}
448
449
override fun onCancel() {
450
Log.d("GameRequest", "Request cancelled")
451
}
452
453
override fun onError(error: FacebookException) {
454
Log.e("GameRequest", "Request error: ${error.message}")
455
}
456
})
457
458
if (GameRequestDialog.canShow()) {
459
dialog.show(content)
460
}
461
}
462
```
463
464
## Open Graph Sharing
465
466
Share Open Graph stories:
467
468
```kotlin { .api }
469
class ShareOpenGraphContent : ShareContent<ShareOpenGraphContent, ShareOpenGraphContent.Builder> {
470
val action: ShareOpenGraphAction
471
val previewPropertyName: String
472
473
class Builder : ShareContent.Builder<ShareOpenGraphContent, Builder>() {
474
fun setAction(action: ShareOpenGraphAction): Builder
475
fun setPreviewPropertyName(previewPropertyName: String): Builder
476
override fun build(): ShareOpenGraphContent
477
}
478
}
479
480
class ShareOpenGraphAction {
481
val actionType: String
482
483
fun putString(key: String, value: String?): ShareOpenGraphAction
484
fun putStringArrayList(key: String, value: ArrayList<String>?): ShareOpenGraphAction
485
fun putObject(key: String, value: ShareOpenGraphObject?): ShareOpenGraphAction
486
fun putObjectArrayList(key: String, value: ArrayList<ShareOpenGraphObject>?): ShareOpenGraphAction
487
488
class Builder(actionType: String, objectKey: String) {
489
fun putString(key: String, value: String?): Builder
490
fun putObject(key: String, value: ShareOpenGraphObject?): Builder
491
fun build(): ShareOpenGraphAction
492
}
493
}
494
495
class ShareOpenGraphObject {
496
fun putString(key: String, value: String?): ShareOpenGraphObject
497
fun putPhoto(key: String, value: SharePhoto?): ShareOpenGraphObject
498
fun putStringArrayList(key: String, value: ArrayList<String>?): ShareOpenGraphObject
499
500
class Builder {
501
fun putString(key: String, value: String?): Builder
502
fun putPhoto(key: String, value: SharePhoto?): Builder
503
fun build(): ShareOpenGraphObject
504
}
505
}
506
```