I would like to add a ShareActionProvider to my ActionBar according to this example. But I can not find any example that sets the Intent that consumes an URL/URI and opens the browser once the user clicks on the menu item.
private ShareActionProvider mShareActionProvider;
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.share_menu, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
To open an url in the browser use the following intent:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Is this what you want?
Related
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>
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);
}
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();
}
}
Logcat Image In android coding of share button got NullPointerException on this line mShareActionProvider.setShareIntent(getDefaultShareIntent());
Can anyone solve this error?
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Now get the ShareActionProvider from the item
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
/** Getting the actionprovider associated with the menu item whose id is share */
// mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return intent;
}
UpDate :
import this
import android.widget.ShareActionProvider;
declare it global
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.menu_main, menu);
// Locate MenuItem with ShareActionProvider
MenuItem shareItem = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider)shareItem..getActionProvider();
setShareIntent(getDefaultShareIntent());
// Return true to display menu
return true;
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
shareIntent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return shareIntent;
}
Due to some dialog issue, i cannot use createChooser method to intent the ACTION_SEND, i have to build my own listview on a standard Activity with share action.
However, i found that only menu item has actionProviderClass property : android:actionProviderClass="android.widget.ShareActionProvider"
How any way i can build my own share action list?
Below is the code how the ShareActionProvider works:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.share_menu, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return intent;
}
You can use PackageManager.queryIntentActivities(Intent, int) to get a list of activities and services that can handle a given Intent.
final List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(shareIntent, 0);
for (Iterator<ResolveInfo> iter = resolveInfos.iterator(); iter.hasNext();) {
ResolveInfo resolveInfo = iter.next();
if (resolveInfo.activityInfo != null) {
final Drawable icon = resolveInfo.loadIcon(PackageManager);
final String title = resolveInfo.loadLabel(PackageManager);
}
}
im having problem implementing the menu to share my app. When ever i open my activity that has the icon share_button, the dialog box "share via" is displayed immediately. I reckon i have problem with this line of code "startActivity(Intent.createChooser(shareIntent(), "Share..."));"
here is my code
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
// Create the share Intent
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
return true;
}
menu item
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:icon="#drawable/ic_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
Change you code like this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareHistoryFileName(null);
return true;
}
and
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_item_share:
String playStoreLink = "https://play.google.com/store/apps/details?id=" +
getPackageName();
String yourShareText = "Install this app " + playStoreLink;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(yourShareText).getIntent();
// Set the share Intent
mShareActionProvider.setShareIntent(shareIntent);
startActivity(Intent.createChooser(shareIntent(), "Share via"));
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
onCreateOptionsMenu will call when your activity will call.
You need to start the share intent when Share button pressed`
So call share Intent in onOptionsItemSelected
You have to move this line of code startActivity(Intent.createChooser(shareIntent(), "Share via")); out into a button click handler or menu select handler.
The function onCreateOptionsMenu() will be called to setup the menu and that will happen when your Activity starts up.
I am having problems with the ShareActionProvider. I like to share the current url of an webview (url can be changed dynamically via JS) via a ShareActionProvider. To do this I overwrote onOptionsItemSelected to change the Intent via setShareIntent. But when I click on the ShareActionProvider and share it via a specific application, nothing happens. I debugged it and I found out that onOptionsItemSelected is not triggered. Can somebody help me out?
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.game, menu);
MenuItem mShareActionProviderItem = (MenuItem) menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) mShareActionProviderItem.getActionProvider(); // is a field
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
mShareActionProvider.setShareIntent(sendIntent); //needed to be able to click on Item
mShareActionProviderItem.getActionView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();
/*ShareButton set URL*/
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_share:
/*URL holen*/
String url = ((GameFragment) getFragmentManager().findFragmentByTag(GAME_FRAGMENT)).getUrl();
/*ShareButton mit URL bestücken*/
ShareActionProvider mShareActionProvider = (ShareActionProvider) item.getActionProvider();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, String.format(getResources().getString(R.string.share_text), url));
mShareActionProvider.setShareIntent(sendIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
I know it's a bit late but I just had the same issue and I solved it by defining a OnShareTArgetSelectedListener on my ShareActionProvider object. :
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider arg0, Intent arg1) {
return false;
}
});
http://developer.android.com/reference/android/widget/ShareActionProvider.html#setOnShareTargetSelectedListener(android.widget.ShareActionProvider.OnShareTargetSelectedListener)