In this tutorial, you will learn to implement an effective and user friendly share action to android app. You can add android share action anywhere in your app. Here I have added share action in actionbar/toolbar and on button’s click event. If user clicks the share button from actionbar/toolbar, many options will be available there to share like email, gmail, facebook, twitter and so on. If you want to add subtitle to your app toolbar/appbar follow Setting Android ActionBar Subtitle and Color tutorial and if you want to make custom material design appbar follow Android Material Design ActionBar/App Bar tutorial.
Here we will share/send message to friends via our android application with message title, description. For subject you need to use EXTRA_SUBJECT and EXTRA_TEXT for text message.
Android Example: How to Add Easy Share Action in Android ActionBar and Button
Create a new android project called android easy share action. Open your app main XML layout file and add a button or textview with onClick attribute. I have added a TextView with onClick attribute. Following is the complete content of XML layout file.
To add share action in android appbar/toolbar you need to create a XML file in res/menu directory and add an items with attributes like android:id, android:actionProviderClass, android:icon, android:title, app:showAsAction. Action_menu.xml file will look like below.
Now open your java activity file and add override onCreateOptionsMenu and onOptionsItemSelected methods to implement share action to android actionbar. In onOptionsItemSelected method you need to add some code to open share option and share something to the friends. If you want to share something when user click the button you need to add little bit code in android button onClick like the below. Following is the complete code of java activity file.
That’s all. Run your Adding a Share Action to Android Application and click on the share icon from action bar or click the share button. There, you will see many option to share, choose one of them and then share to your friends.