I have the following code to open the share Intent but it's disabled and unable to click.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.action_share:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
File folder = new File(Environment.getExternalStorageDirectory() + "/TESTFOLDER");
if (!folder.exists()) {
folder.mkdir();
}
try {
if (file.exists()) {
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivityForResult(Intent.createChooser(share, "Share Chart"), 1);
}
else {
//displayToast("Please save your image before sharing.");
}
}
catch (Exception e) {
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
share xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_share"
android:showAsAction="always"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
The share icon is disabled in my action bar:
That is not how you use the
ShareActionProvider
.You call
setShareIntent()
on theShareActionProvider
as soon as you have the data to share. This method takes anIntent
object representing the share operation -- basically what you have as theshare
local variable. At that point, the provider in the action bar will be clickable, assuming that there are applications on your device capable of sharing based upon your chosenIntent
.Also, you do not need (or want to have) the
R.id.action_share
case inonOptionsItemSelected()
, asShareActionProvider
does not need it, and it may interfere with proper operation.For example, here is an activity that implements a
ShareActionProvider
to share text entered into theEditText
that makes up the content view: