# ShareActionProvider

Artifact: [androidx.appcompat:appcompat](https://developer.android.com/jetpack/androidx/releases/appcompat) [View Source](https://cs.android.com/search?q=file:androidx/appcompat/widget/ShareActionProvider.java+class:androidx.appcompat.widget.ShareActionProvider) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

*** ** * ** ***


```
class ShareActionProvider : ActionProvider
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.core.view.ActionProvider](https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider) ||
|   | ↳ | [androidx.appcompat.widget.ShareActionProvider](https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider) |

*** ** * ** ***

Provides a share action, which is suitable for an activity's app bar. Creates views that enable data sharing. If the provider appears in the overflow menu, it creates a submenu with the appropriate sharing actions.

### Adding a share action

To add a "share" action to your activity, put a `ShareActionProvider` in the app bar's menu resource. For example:

```kotlin
<item android:id="@+id/action_share"
     android:title="@string/share"
     app:showAsAction="ifRoom"
     app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider"/>
```

You do not need to specify an icon, since the `ShareActionProvider` widget takes care of its own appearance and behavior. However, you do need to specify a title with `android:title`, in case the action ends up in the overflow menu.

Next, set up the intent that contains the content your activity is able to share. You should create this intent in your handler for `https://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)`, and update it every time the shareable content changes. To set up the intent:

1. Get a reference to the ShareActionProvider by calling `https://developer.android.com/reference/android/view/MenuItem.html#getActionProvider()` and passing the share action's `https://developer.android.com/reference/android/view/MenuItem.html`. For example:

   ```kotlin
   MenuItem shareItem = menu.findItem(R.id.action_share);
   ShareActionProvider myShareActionProvider =
   (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
   ```
2. Create an intent with the `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND` action, and attach the content shared by the activity. For example, the following intent shares an image:

   ```kotlin
   Intent myShareIntent = new Intent(Intent.ACTION_SEND);
   myShareIntent.setType("image/*");
   myShareIntent.putExtra(Intent.EXTRA_STREAM, myImageUri);
   ```
3. Call `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareIntent(android.content.Intent)` to attach this intent to the action provider:

   ```kotlin
   myShareActionProvider.setShareIntent(myShareIntent);
   ```
4. When the content changes, modify the intent or create a new one, and call `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareIntent(android.content.Intent)` again. For example:

   ```kotlin
   // Image has changed! Update the intent:
   myShareIntent.putExtra(Intent.EXTRA_STREAM, myNewImageUri);
   myShareActionProvider.setShareIntent(myShareIntent);
   ```

### Share target rankings

The share action provider retains a ranking for each share target, based on how often the user chooses each one. The more often a user chooses a target, the higher its rank; the most-commonly used target appears in the app bar as the default target.

By default, the target ranking information is stored in a private file with the name specified by `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#DEFAULT_SHARE_HISTORY_FILE_NAME()`. Ordinarily, the share action provider stores all the history in this single file. However, using a single set of rankings may not make sense if the share action provider is used for different kinds of content. For example, if the activity sometimes shares images and sometimes shares contacts, you would want to maintain two different sets of rankings.

To set the history file, call `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String)` and pass the name of an XML file. The file you specify is used until the next time you call `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String)`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider` |   |

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider.OnShareTargetSelectedListener` Listener for the event of selecting a share target. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#DEFAULT_SHARE_HISTORY_FILE_NAME() = "share_history.xml"` The default name for storing share history. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#ShareActionProvider(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html!)` Creates a new instance. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#hasSubMenu()()` Determines if this ActionProvider has a submenu associated with it. |
| `https://developer.android.com/reference/android/view/View.html!` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#onCreateActionView()()` Factory method for creating new action views. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#onPrepareSubMenu(android.view.SubMenu)(subMenu: https://developer.android.com/reference/android/view/SubMenu.html!)` Called to prepare an associated submenu for the menu item backed by this ActionProvider. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setOnShareTargetSelectedListener(androidx.appcompat.widget.ShareActionProvider.OnShareTargetSelectedListener)( listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider.OnShareTargetSelectedListener! )` Sets a listener to be notified when a share target has been selected. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String)(shareHistoryFile: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!)` Sets the file name of a file for persisting the share history which history will be used for ordering share targets. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareIntent(android.content.Intent)(shareIntent: https://developer.android.com/reference/android/content/Intent.html!)` Sets an intent with information about the share action. |

| ### Inherited functions |
|---|
| From [androidx.core.view.ActionProvider](https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider) |---|---| | `https://developer.android.com/reference/android/content/Context.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#getContext()()` Gets the context associated with this action provider. | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#isVisible()()` If `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#overridesItemVisibility()` returns true, the return value of this method will help determine the visibility of the `https://developer.android.com/reference/android/view/MenuItem.html` this ActionProvider is bound to. | | `https://developer.android.com/reference/android/view/View.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#onCreateActionView(android.view.MenuItem)(forItem: https://developer.android.com/reference/android/view/MenuItem.html)` Factory method called by the Android framework to create new action views. | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#onPerformDefaultAction()()` Performs an optional default action. | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#overridesItemVisibility()()` The result of this method determines whether or not `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#isVisible()` will be used by the `https://developer.android.com/reference/android/view/MenuItem.html` this ActionProvider is bound to help determine its visibility. | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#refreshVisibility()()` If this ActionProvider is associated with an item in a menu, refresh the visibility of the item based on `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#overridesItemVisibility()` and `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#isVisible()`. | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider#setVisibilityListener(androidx.core.view.ActionProvider.VisibilityListener)(listener: https://developer.android.com/reference/kotlin/androidx/core/view/ActionProvider.VisibilityListener?)` Set a listener to be notified when this ActionProvider's overridden visibility changes. | |

## Constants

### DEFAULT_SHARE_HISTORY_FILE_NAME

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
const val DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml": String!
```

The default name for storing share history.

## Public constructors

### ShareActionProvider

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
ShareActionProvider(context: Context!)
```

Creates a new instance.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | Context for accessing resources. |

## Public functions

### hasSubMenu

```
fun hasSubMenu(): Boolean
```

Determines if this ActionProvider has a submenu associated with it.

Associated submenus will be shown when an action view is not. This provider instance will receive a call to #onPrepareSubMenu(SubMenu) after the call to \* #onPerformDefaultAction() and before a submenu is displayed to the user.

### onCreateActionView

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun onCreateActionView(): View!
```

Factory method for creating new action views.

### onPrepareSubMenu

```
fun onPrepareSubMenu(subMenu: SubMenu!): Unit
```

Called to prepare an associated submenu for the menu item backed by this ActionProvider.

if #hasSubMenu() returns true, this method will be called when the menu item is selected to prepare the submenu for presentation to the user. Apps may use this to create or alter submenu content right before display.

### setOnShareTargetSelectedListener

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun setOnShareTargetSelectedListener(
    listener: ShareActionProvider.OnShareTargetSelectedListener!
): Unit
```

Sets a listener to be notified when a share target has been selected. The listener can optionally decide to handle the selection and not rely on the default behavior which is to launch the activity.

**Note:** If you choose the backing share history file you will still be notified in this callback.

| Parameters |
|---|---|
| `listener: https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider.OnShareTargetSelectedListener!` | The listener. |

### setShareHistoryFileName

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun setShareHistoryFileName(shareHistoryFile: String!): Unit
```

Sets the file name of a file for persisting the share history which history will be used for ordering share targets. This file will be used for all view created by `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#onCreateActionView()`. Defaults to `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#DEFAULT_SHARE_HISTORY_FILE_NAME()`. Set to `null` if share history should not be persisted between sessions.

**Note:** The history file name can be set any time, however only the action views created by `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#onCreateActionView()` after setting the file name will be backed by the provided file. Therefore, if you want to use different history files for sharing specific types of content, every time you change the history file with `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/ShareActionProvider#setShareHistoryFileName(java.lang.String)` you must call `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatActivity#supportInvalidateOptionsMenu()` to recreate the action view. You should **not** call `https://developer.android.com/reference/kotlin/androidx/appcompat/app/AppCompatActivity#supportInvalidateOptionsMenu()` from `https://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)`.

```kotlin
private void doShare(Intent intent) {
    if (IMAGE.equals(intent.getMimeType())) {
        mShareActionProvider.setHistoryFileName(SHARE_IMAGE_HISTORY_FILE_NAME);
    } else if (TEXT.equals(intent.getMimeType())) {
        mShareActionProvider.setHistoryFileName(SHARE_TEXT_HISTORY_FILE_NAME);
    }
    mShareActionProvider.setIntent(intent);
    supportInvalidateOptionsMenu();
}
```

| Parameters |
|---|---|
| `shareHistoryFile: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | The share history file name. |

### setShareIntent

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0)

```
fun setShareIntent(shareIntent: Intent!): Unit
```

Sets an intent with information about the share action. Here is a sample for constructing a share intent:

```kotlin
 Intent shareIntent = new Intent(Intent.ACTION_SEND);
 shareIntent.setType("image/*");
 Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
 shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
```

| Parameters |
|---|---|
| `shareIntent: https://developer.android.com/reference/android/content/Intent.html!` | The share intent. |

| See also |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND` |   |
| `https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE` |   |