I am trying to get a working "share" button on the title bar. Initially, I followed the advice on developers.android.com for this, but got an exception saying that I needed to use MenuItemCompat. I am doing so, but now my share button won't even "click".
My relevant code is:
public boolean onCreateOptionsMenu(Menu menu) {
Log.d("CYCLE:","onCreateOptionsMenu()");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_to_do_list, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.action_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
setShareIntent();
mShareActionProvider.setShareHistoryFileName("my_share_history.xml");
return true;
}
public void setShareIntent(){
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "some text to share");
mShareActionProvider.setShareIntent(shareIntent);
}
and my menu item in XML is:
<item android:id="#+id/action_share"
android:title="Share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
am I missing something to make this button be clickable? (I am not even talking about functionality yet. Simply the ability to be clicked (you know, like with the clicking/tapping animation).
thanks.
Have you set share Intent? Without the share Intent, Android would not know what exactly need to be shared by the provider.
Related
I'm using a ShareActionProvider(android.widget.ShareActionProvider) to share simple text.
It works fine with Gmail, WhatsApp etc but not with Facebook...
It doesn't share the text attached to the Intent, instead it just ask the user to write a new post.
How to solve this?
Here is my code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:id="#+id/action_share"
android:title="#string/action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
Java:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.detailfragment, menu);
// Retrieve the share menu item
MenuItem share = menu.findItem(R.id.action_share);
// Get the provider and hold onto it to set/change the share intent.
mShareActionProvider = (ShareActionProvider) share.getActionProvider();
// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
if (mShareActionProvider != null && mForecastStr != null ) {
mShareActionProvider.setShareIntent(createShareIntent());
Log.v(LOG_TAG, "mForecast: " + mForecastStr);
Log.v(LOG_TAG, "Intent: " + mShareActionProvider);
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
public Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr);
return shareIntent;
}
Thanks!!
Unfortunately, you can't solve it. Facebook does not handle the EXTRA_TEXT field. You can check this bug report page.
There is a middle solution. It is true FB doesn't share TEXTS. They say that it is an attack to the freedom of expression of the User. But, you have a solution. You may pass a link. Then FB translate the "Intent" to a image and a link, and it added to the future response. But it leave blank the User writing space...
For example try:
shareIntent.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");
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 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 UIL to display images in a gridview. When one is selected it displays in a fullscreen window (UIL's ImagePagerActivity class) This screen allows you to flip through the available pictures. I want to use the ShareActionProvider in the action bar to share the currently displayed picture. I keep getting a null pointer exception when loading the onCreateOptionsMenu where it sets the shareIntent. I think it's not finding the displayed picture. Can someone look over this and help?
XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_share"
android:actionProviderClass="android.suport.v7.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="Share"/>
</menu>
Android
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.album_menu, menu);
MenuItem item = menu.findItem(R.id.menu_item_share);
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
mShareActionProvider.setShareIntent(getDefaultIntent());
return true;
}
private Intent getDefaultIntent() {
Bitmap cachedImage = imageLoader.getMemoryCache().get(imageUrls[pagerPosition]);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, cachedImage);
return intent;
}
I Action Bar i am using ShareActionProvider, but facing small issue, i am also getting Messenger Icon along with Share Icon, and i don't want to show Messenger Icon in my Action Bar.
So how can i remove Messenger Icon from Action Bar, see my code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(getDefaultShareIntent());
}
return super.onCreateOptionsMenu(menu);
}
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, "TEXT");
return intent;
}
I just want to show Share Icon in my Action Bar not Messenger Icon...
see screen shot:
You are welcome to try setShareHistoryFileName(null) and see if that blocks the second icon along with the history.
Otherwise, you will need to write your own action provider as a replacement for ShareActionProvider. Or, you will need to attempt to fork ShareActionProvider to block this second icon.
Create a menu in layout in this way:
<item
android:showAsAction="always"
android:icon="#drawable/share_icon"/>
When you click on it you can show the default sharing sources.