I am trying to click on the ShareActionProvider button on my mobile, but it is completely unresponsive. None of the sharing apps such as texting or emailing pop up. I've labeled the ShareActionProvider as menu_share in my xml file, and am using the method onOptionsItemSelected to respond to any clicks.
Java
import android.support.v7.widget.ShareActionProvider;
public class MainActivityFragment extends Fragment{
ArrayAdapter<String> mForecastAdapter;
//String[] parsedWeatherData;
ShareActionProvider provider;
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.forecastfragment, menu);
MenuItem item = menu.findItem(R.id.menu_share);
provider = (ShareActionProvider)
MenuItemCompat.getActionProvider(item);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
Log.d("Weather", "Is menu share clicked");
doShare();
break;
default:
break;
}
return true;
}
public void doShare() {
String message = oneDayWeather;
// populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, message);
provider.setShareIntent(intent);
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bwq="http://schemas.android.com/apk/res-auto"
>
<item
android:id="#+id/menu_share"
android:title="#string/menu_share"
bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
bwq:showAsAction="always"/>
</menu>
Any ideas?
Never mind, I solved it. This version of ShareActionProvider only works if you set the setShareIntent method right after getting the provider. For example:
MenuItem item = menu.findItem(R.id.menu_share);
provider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
provider.setShareIntent(getDefaultShareIntent());
Related
I added a ShareActionProvider menu item to my add, but when I press the "Share" button, the onOptionsItemSelected function doesn't capture the event, and I don't succeed to capture the changes in the text (the shareContent is initialized with the onCreate data through the onCreateOptionsMenu function).
I found in old posts that in older versions (support.v7), the "MenuItemCompat" needed to be used, but it's not recognized in android.widget.ShareActionProvider class.
This is part of my code:
menu.xml:
<item
android:id="#+id/action_share"
android:title="#string/action_title_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always" />
mainActivity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
setShareIntent(createShareIntent());
return true;
}
#Override
public boolean onOptionsItemSelected (MenuItem item)
{
switch (item.getItemId() )
{
case R.id.action_share:
//share note content
noButtonWasPressed = false;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
setShareIntent(createShareIntent());
return true;
Any suggestions?
I'm trying to implement a share intent to share images. I have a full screen activity which extends ActionBarActivity and a fragment that implements the immersivemode as the android developer's guide explains, so the user can see the image in fullscreen. The problem is that even if I can see the share icon on ActionBar, it seems that it can't be clicked, apparently because the method onOptionsItemSelected is never called.
Here's the onCreateOptionsMenu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_share, menu);
return true;
}
onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.menu_share:
Intent i = getIntent();
uriString = i.getStringExtra("uri");
if(uriString != null) {
Uri uri = Uri.parse(uriString);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
mShareActionProvider.setShareIntent(createShareIntent(uri));
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Here's the method I created to prepare and start the intent:
private Intent createShareIntent(Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_image)));
return shareIntent;
}
menu_share.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_share"
android:title="Share"
app:showAsAction="ifRoom"
android:icon="#drawable/abc_ic_menu_share_mtrl_alpha"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
/>
</menu>
I've tried different ways, for instance using setOnMenuItemClickListener or similar things which consider listeners, but I see that these listeners are ignored when I debug. I've also searched for a solution here and on other websites but I can't get how to solve this problem, so any idea will be appreciated.
Thanks in advance!
You are inflating the wrong menu. Since your menu file is called menu.xml you should do a
getMenuInflater().inflate(R.menu.menu, menu);
You dont directly inflate an entry, but the menu.
Also below your
switch (item.getItemId()) {
case R.id.menu_share:
write: Log.d("mytag", "share menu clicked"); Then you can verify in your android logcat, that there is nothing wrong with the menu clicking, but with the Intent you are receiving from.
Eventually, I changed my approach to solve the problem and now I'm able to call the onOptionsItemSelected(MenuItem item) method and share images from my app to the others. I put the code below.
onCreateOptionsMenu(Menu menu):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menuItem = menu.add(Menu.NONE, R.id.action_share, Menu.NONE, R.string.action_share);
menuItem.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
mShareActionProvider = new ShareActionProvider(this);
return true;
}
onOptionsItemSelected(MenuItem item):
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_share:
Intent i = getIntent();
uriString = i.getStringExtra("uri");
if (uriString != null) {
Uri uri = Uri.parse(uriString);
MenuItemCompat.setActionProvider(item, mShareActionProvider);
createShareIntent(uri);
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
My method to create the share intent when I click on the right menu item:
private void createShareIntent(Uri uri) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
The id of the item is into ids.xml in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="action_share"/>
</resources>
strings.xml:
<!-- Actions -->
<string name="action_share">Share image</string>
As you can see, I don't use getMenuInflater().inflate(int menuRes, Menu menu) anymore, but I use the add() method to generate my menu and it works for me.
I have an app, and I'd like to add a share button to it. Once the button is clicked, I'd like it to open the following window:
Then the user will choose where to share it and it will display the following default message:
"Just found this great app! Find it here: https://play.google.com/store/apps/details?id=com.ideashower.readitlater.pro"
Solution 1: Launch ACTION_SEND Intent
When launching a SEND intent, you should usually wrap it in a chooser (through createChooser(Intent, CharSequence)), which will give the proper interface for the user to pick how to send your data and allow you to specify a prompt indicating what they are doing.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
# change the type of data you need to share,
# for image use "image/*"
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, URL_TO_SHARE);
startActivity(Intent.createChooser(intent, "Share"));
Solution 2: Use ShareActionProvider
If you are just looking to add a Share button in Overflow Menu, also have a look at ShareActionProvider.
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
MenuItem item = menu.findItem(R.id.share_item);
actionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
String shareText = URL_TO_SHARE;
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setType("text/plain").setText(shareText).getIntent();
actionProvider.setShareIntent(shareIntent);
return true;
}
Hope this helps. :)
As explained on Android Developers on this link: http://developer.android.com/training/sharing/shareaction.html
you have to add this menu item:
<item
android:id="#+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
Then add the following code in Activity:
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);
}
}
I'm using a menu item on the action bar and I want to share my app by clicking the share icon. When I click the share icon it doesn't work. Also, I want to add text saying "install this app" when shared.
Here is my code:
private ShareActionProvider mShareActionProvider;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
return true;
}
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
Mainpage.xml menu:
<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_store"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
If you want a static share Intent (i.e., it never changes), then you update your onCreateOptionsMenu to be
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// 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);
return true;
}
you can get the official tutorial here http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider
In the snappet you paste, you forget to call
mShareActionProvider.setShareIntent(intent);
I am using Sherlock Library Action Bar, which is similar to the ICS action bars, the place where I am stuck is this
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_compose).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Search")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
SubMenu sub = menu.addSubMenu("Options");
sub.add(0, SubMenu.NONE, 0, "First");
sub.add(0,SubMenu.NONE, 1, "Second");
sub.add(0, SubMenu.NONE, 2, "Three");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// HERE IS WHere I AM FACING PROBLEM IN
getSupportMenuInflater().inflate(R.menu.share_action_provider, menu);
// Set file with share history to the provider and set the share intent.
actionItem = menu.findItem(R.id.menu_item_share_action_provider_action_bar);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); //this is BRILLIANT WAY TO AVOID REPEATation
actionProvider.setShareIntent(createShareIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//THIS TEXT IS WHAT I WANT TO OBTAIN DYNAMICALLY
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Something goes here");
return shareIntent;
}
Whenever I try putting the actionProvider.setShareIntent(createShareIntent()); inside a listener or anywhere other than the actionProvider.setShareIntent(createShareIntent()); I get D/View(11753): onTouchEvent: viewFlags is DISABLED
I wish to include this sharing Action provider and want to input my own text that gets generated on user inputs.
Any inputs are welcome.
Adding this doesn't work
actionItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.e("THIS IS NEVER CALLED", "??");
return true;
}
});
How do I get an update for the Share button being clicked
I hit a similar issue with a android.support.v7.app.ActionBarActivity derivative (though I think this approach should work for any activity) and I overrode onPrepareOptionsMenu and got the effect you were after:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
if (actionProvider != null) {
actionProvider.setShareIntent(createShareIntent());
}
return super.onPrepareOptionsMenu(menu);
}
i use menu like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.your_share_button_name:
share();
return true;
}
}
private void share() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"your text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"your subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
that's all. I suggest you to put all the layout of the menu in a xml in your res/menu folder.