Reputation: 189
My app has a share button that shares a link to the app on the play-store. I'm new to the intent method and keep getting this error:
And this error
Any input on how to correctly create a share button is greatly apreciated! Below is the code in ActivityMain and below that; the XML to the share button.
private ShareActionProvider shareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.nav_share);
// Fetch and store ShareActionProvider
shareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (shareActionProvider != null) {
shareActionProvider.setShareIntent(shareIntent);
}
SHARE BUTTON XML
<item android:title="Communication">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_share" />
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
</item>
Upvotes: 0
Views: 1803
Reputation: 742
Here is sample code:
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="@string/menu_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.main, menu);
return true;
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.nav_share);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.nav_share){
ShareApp();
}
return false;
}
});
// Fetch and store ShareActionProvider
shareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
private void ShareApp() {
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
String shareMessage = "\nLet me recommend you application\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID + "\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch (Exception e) {
DebugLog.e(e.getMessage());
e.printStackTrace();
}
}
Upvotes: 1
Reputation: 915
On your menu item / button click call the following method.
private void shareAppLink(){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_VIEW);
shareIntent.setData(Uri.parse("Link to app store"));
startActivity(shareIntent);
}
Upvotes: 3