Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Created June 7, 2015 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovalney/4bb5b00aa507d73ff3ae to your computer and use it in GitHub Desktop.
Save mariovalney/4bb5b00aa507d73ff3ae to your computer and use it in GitHub Desktop.
Piece of code ("onOptionsItemSelected" method) to share anything in a Android Activity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "TEXTO A SER COMPARTILHADO");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Compartilhe com..."));
return true;
}
return super.onOptionsItemSelected(item);
}
@mariovalney
Copy link
Author

Remember: you need a menu_layout.xml file like that:

<?xml version="1.0" encoding="utf-8"?>

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/action_share"
        android:title="@string/action_share"
        android:icon="@drawable/ic_action_share"
        app:showAsAction="always" />

    <item android:id="@+id/action_about"
        android:icon="@drawable/ic_action_about"
        android:title="@string/action_about"
        android:orderInCategory="100"
        app:showAsAction="ifRoom" />
</menu>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment