Learn How to Share Content using an Action Provider

In this Article you will learn how to add a Share feature to your App.

This will enable users of your app to share content. For instance a user might want to share an image being displayed on the App, using a Messenger or an Email.

In order to do this you need to add an Action Provider to the Action Bar.

Here are the steps:

1. Add a share action provider to the menu xml file.

<item
android:id=”@+id/action_share”
android:title=”@string/action_share”
android:orderInCategory=”2″
android:showAsAction=”always”
android:actionProviderClass=”android.widget.ShareActionProvider” />

2. Specify the content to be passed using an intent in your Activity.

Please refer to the code here

Learn about the Android Menu onCreateOptionsMenu and onOptionsItemSelected methods

If you want to add Menu Items to the Action Bar then you will have to override two methods in your Activity Class.

  • onCreateOptionsMenu – To display Menu
  • onOptionsItemSelected – To handle user clicks

Follow the video to understand this better.

Learn how to add Action Items to the Action Bar

You should now be familiar with the Android Themes from the last Article.

Action Bars can be used to add items or shortcuts. Like if the user wants to quickly search for something or add something or even modify the app settings.

Let’s see how you can add such functionality.

There are three primary components which needs to be modified.

1. Menu resource file for the activity.
2. The onCreateOptionsMenu method.
3. The onOptionsItemSelected method.

You can find the menu resource file in the /menu folder. It’s auto generated by the Android Studio when you create a new Project.

The other two methods needs to be overridden in the Activity.

Follow the video to understand how to add this feature. Source code is available here.

Note : For the ThemeDemoApp you need to first remove the v7 dependency, as it creates a conflict when you try to use showAsAction attribute.