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);
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 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());
My problem is when I use share event at that time, my first activity get closed
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);
MenuItem shareItem = (MenuItem) menu.findItem(R.id.action_share);
ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");`enter code here`
String Scategory_id = global.getcategoryid();
}
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider"/>
ABOVE CODE APPLY IN MENU. XML AND I GET THE PROBLEM WHEN I USE SHARE INTENT MY MAIN APPLICATION GET CLOSED ACTCHULY ITS AFTER USING SHARE BUTTON ITS SHOULD BE REDIRECT ON MY FIRST APPLICATION
You need to call setShareIntent on mShare.
mShare.setShareIntent(shareIntent);
I try to use ShareActionProvider. It's work but i want put a different text when the user click on Twitter.
How can i fix it?
This my code, the object News contents a title and a url
When he clicks on Twitter :
#account_name myText
And for other
myText
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail_news, menu);
MenuItem item = menu.findItem(R.id.action_share);
shareActionProvider = (ShareActionProvider) item.getActionProvider();
shareActionProvider
.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
shareActionProvider.setShareIntent(createShareIntent());
return true;
}
private Intent createShareIntent() {
// Here, is Twitter, add "#Televesdre" in the message
News news = detailAdapter.getCurrentItem(viewPager.getCurrentItem());
String message = news.getTitle() + "\n\n Article: " + news.getUrl();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
return shareIntent;
}
I search how to do before coding because i haven't no idea how to do
Try ShareActionProvider.OnShareTargetSelectedListener
I have two different Menu items (Flipping and Sharing) that share a single activity. Each one of them, works perfectly and smooth by its own, but when I put them together, the flipping action takes too long to respond. What can I do? Thanks for your help.
Flipping action:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Add either a "photo" or "finish" button to the action bar, depending on which page
// is currently selected.
MenuItem item = menu.add(Menu.NONE, R.id.action_flip, Menu.NONE,
mShowingBack
? R.string.action_photo
: R.string.action_info);
item.setIcon(mShowingBack
? R.drawable.ic_action_photo
: R.drawable.ic_action_info);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Share Action
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onPrepareOptionsMenu(menu);
}
I left this question open, and I forgot about it. However, I took a look again and remember how I solve it. I was doing it all wrong. I had to call super.onPrepareOptionsMenu(menu) before everything like this:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
getMenuInflater().inflate(R.menu.share_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return true;
}