Skip to content

Instantly share code, notes, and snippets.

@nein37
Last active October 29, 2015 03:58
Show Gist options
  • Save nein37/873f4fd9ee11f0572d0c to your computer and use it in GitHub Desktop.
Save nein37/873f4fd9ee11f0572d0c to your computer and use it in GitHub Desktop.
ActionBarの共有ボタンに共有履歴を表示する方法 ref: http://qiita.com/nein37/items/cf5af57f4a313958631d
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyActivity">
<item
android:id="@+id/action_share_always"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always"
android:title="@string/action_share" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
// ShareActionProvider の設定
MenuItem actionItem = menu.findItem(R.id.action_share_always);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
// インテントの設定
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("hogehoge")
.setType("text/plain")
.getIntent();
// ShareActionProviderにインテントの設定
actionProvider.setShareIntent(shareIntent);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 何も書かなくてOK
return super.onOptionsItemSelected(item);
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyActivity">
<item
android:id="@+id/action_share_always"
android:icon="@android:drawable/ic_menu_share"
android:title="共有"
app:showAsAction="always" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
// IntentBuilderの生成
ShareCompat.IntentBuilder builder = ShareCompat.IntentBuilder.from(this);
builder.setText("hogehoge")
.setType("text/plain");
// MenuItemにShareアクションの設定
ShareCompat.configureMenuItem(menu, R.id.action_share_always, builder);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment