The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Tutorial #15 - Sharing Your Notes

Perhaps you would like to get your notes off of our book reader app and into someplace else, or perhaps you would like to share them with somebody else. Either way, we can do that using an ACTION_SEND operation, to allow the user to choose how to “send” the notes, such as sending them by email or uploading them to some third-party note service.

To make this work, we will add a ShareActionProvider to our action bar on the NoteFragment.

This is a continuation of the work we did in the previous tutorial.

You can find the results of the previous tutorial and the results of this tutorial in the book’s GitHub repository.

Step #1: Adding a ShareActionProvider

First, we need to allow the user to indicate that they want to “share” the note displayed in the current NoteFragment. By putting an action bar item on the activity where the NoteFragment is displayed, we do not need to worry about letting the user choose which note to send — we simply send whichever note they happen to be viewing or editing.

By using a ShareActionProvider, the action item will handle most of the work for allowing the user to choose where to send the note to. We only need to provide an Intent that identifies what is to be shared.

Modify res/menu/notes.xml to add in the new share toolbar button:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/share"
    android:actionProviderClass="android.widget.ShareActionProvider"
    android:showAsAction="ifRoom"
    android:title="@string/share"/>
  <item
    android:id="@+id/delete"
    android:icon="@drawable/ic_delete_white_24dp"
    android:showAsAction="ifRoom|withText"
    android:title="@string/delete">
  </item>
</menu>

Note that this menu definition requires a new string resource, named share, with a value like Share.

Step #2: Sharing the Note

The preview of this section is [REDACTED].

Step #3: Testing the Result

The preview of this section is in an invisible, microscopic font.

In Our Next Episode…

The preview of this section is [REDACTED].