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.
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();
}
}
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?
I have problem. I was follow a lot of guides, but still can't get my Share button to work. I got the icon showing in ActionBar, but when I press nothing is happening when android:showAsAction="always". But when android:showAsAction="never" it works. I just want share icon to be always shown in ActionBar. What I am doing wrong? Please help
Here is my code:
public class Main extends Activity {
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.shareButton);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.shareButton:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Check it out";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
return true;
case R.id.aboutButton:
Intent intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="#string/share"
android:showAsAction="always"/> **---> when I put here "NEVER" then it works! But I want Share to be always as icon**
<item
android:id="#+id/aboutButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="never"
android:title="#string/about_dev"/>
</menu>
If you didn´t find a solution try this to get the ShareActionProvider
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share)
.getActionProvider();
mShareActionProvider.setShareIntent(doShare());
return true;
}
And doShare() would be:
public Intent doShare() {
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
return intent;
}
Try "Intent share = new Intent (Intent.ACTION_SEND)" instead.
You seem to be doing everything right!
In
case R.id.shareButton:
instead of using
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
use:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
mShareActionProvider.setShareIntent(sharingIntent);
Refer the following links for more information:
https://developer.android.com/reference/android/widget/ShareActionProvider.html
https://developer.android.com/training/sharing/shareaction.html#set-share-intent
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)
I'm writing a recipe book, and I've encountered a problem - when I click my menu item, which is supposed to send email, with email address and subject pre-filled, nothing happens ...
Any ideas why ???
public class recipedisplayscreen extends Activity {
TextView EmailAddress;
TextView EmailSubject;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipedisplayscreen);
TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);
Intent i = getIntent();
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
Log.e("recipedisplayscreen", Ingredients + "." + Method);
MethodDisplay.setText(Method);
IngredientsDisplay.setText(Ingredients);
EmailAddress=(TextView) findViewById(R.id.textView2);
EmailSubject=(TextView) findViewById(R.id.textView4);
ActionBar actionBar = getActionBar();
setTitle(R.string.title);
actionBar.setDisplayHomeAsUpEnabled(true);
setTitle(Method);}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onOptionsItemSelected1(MenuItem recipe_suggest) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.recipe_menu1, menu);
return true;
}
}
I believe this is what you want :D delete onOptionsItemSelected1 and replace onOptionsItemSelected with this, this is assuming that recipe_suggest is the menu item's id?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.recipe_suggest:
//Other menu item I believe
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
What a switch does is match the object in the "switch" to the case, the case that matches the "switch" gets carried out, so if the menu item id (MenuItem item; item.getItemId()) matches the ID either android.R.id.home or R.id.recipe_suggest (I assume you are using the action bar as you are referring to the Android R file?) this is how it should be done to the best of my knowlegde :)
Because you have two onOptionsItemSelected? Well, one is called onOptionsItemSelected*1* , need to merge that into the real one.
You can not just create a method called onOptionsItemSelected1() and expect that android calls it when you hit the menu entry to send the mail.
Merge this method to the original onOptionsItemSelected() and put it in the right case, than it should work.
Why do you have a separate method called onOptionsItemSelected*1* ?
All the code reacting to action items should be in onOptionsItemSelected :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case android.R.id.email_action_item:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
(...)
default:
return super.onOptionsItemSelected(item);
}
}