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);
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 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.
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 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.
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);