It's a example to share plain text using ShareActionProvider.
![Implement ShareActionProvider for Android 4 Implement ShareActionProvider for Android 4](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_zH8SGB9_InauI6SmaQC7rNlVK3_n9JdhKA7TXY29Dkw6_UOaLCjcd3mZV9tMWl8QqmYlge8JNQCpP5wL8OmxnhjViq642Sh21lb5ShEZ6L7fPXtke0RHaoxDXUDzZbQUWB2wOMtQa0te/s400/AndroidShareActionProvider_01.png)
With share history:
![With share history With share history](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLc7cZf1-a-CdgI-sX_BvkkBywACYlRTMfcTkBeyaccbR1iNLEASOZuZl4hjv8WFd4IaTjRN4Fnm_MNNrfJtvXnYu5OLNpkOJ1QJmDD9kxuITOzexCzdr6DPakl305GGLk9y2wrAflEdF4/s400/AndroidShareActionProvider_02.png)
Create /menu/menu.xml to define our ActionBar, with Share action only.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
Main Java code:
package com.exercise.AndroidShareActionProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
public class AndroidShareActionProviderActivity extends Activity {
private ShareActionProvider myShareActionProvider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
myShareActionProvider = (ShareActionProvider)item.getActionProvider();
myShareActionProvider.setShareHistoryFileName(
ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
myShareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,
"http://android-er.blogspot.com/");
return shareIntent;
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8p8OYSJ34x9VZ9cj4IzAOVtRlBOzcLrPRJWQ4iuiJI1eCKjrLYOEL2p2RYTNBidfvJuDOpoiZjXICYCF5ifVUBp09IMGbHsHFSOko1_Xdf4gYrG9MbcBSZDywv5SWAQhCav5OT9OGPE5T/s200/icons_download.gif)
4 comments:
Is there a way to disable share history and remove the frequently used share app icon on the right?
how to delete the share history i only want the plain text ShareActionProvider
Do you have a sample using the support library?
how to share current text on that screen
Post a Comment