Android: SharingActionView on swipe from bottom - android

I would like to share data with other Apps, but I don't want the standard popup which appears bottom tool bar; I want this kind of view:
I followed the official document: https://developer.android.com/training/sharing/shareaction.html
So is there a native component to do that in Android SDK? Or is there any library to do that?
thank for your help guys!

You can do it like this :
First create a menu item :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.rashish.myapplication.MainActivity">
<item
android:id="#+id/action_share"
android:orderInCategory="100"
android:title="#string/action_settings"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="always" />
</menu>
Then create a method to show the Intent chooser dialog, like this :
private void share() {
String textToShare = "hello";
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
// set the type here i.e, text/image/ etc.
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, textToShare);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
Then in the onOptionsItemSelected method, call this function when the menu item is clicked :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here.
int id = item.getItemId();
if (id == R.id.action_share) {
share();
return true;
}
return super.onOptionsItemSelected(item);
}
Also make sure you've overriden onCreateOptionsMenu in your Activity like this :
#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_main, menu);
return true;
}
Here's the output :
screenshot from an android device

In Android OS Lollipop google has change their share intent design. So to get this design you should have lollipop version and above.
Update:
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
File photoFile = new File(getFilesDir(), "foo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
mShareActionProvider.setShareIntent(shareIntent);
}
}
try like this.

Related

MenuItem on ActionBar can't be clicked

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.

ShareActionProvider doesn't share text from Intent with Facebook

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

Android Sherlock Actionbar onOptionsItemSelected is not called

I m using Sherlock Actionbar to show share option in Action Bar of my application.
The Share option works fine.
I want to get the option which user selects from Share option, so that I can share
different text for facebook, messages, mail etc.
For this I am using onOptionsItemSelected function but this function
is never called.
Please help me to fix this or is there any workaround
to achieve this. Thanks..
public class MainActivity extends SherlockActivity {
private ShareActionProvider mShareActionProvider;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getSupportMenuInflater().inflate(R.menu.items, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
Intent intent = getDefaultShareIntent();
if(intent!=null)
mShareActionProvider.setShareIntent(intent);
return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
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,"Sample Content !!!");
return intent;
}
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
System.out.println("Testing...................");
return false;
}
}
Items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/share"
android:title="#string/share"
android:showAsAction="always"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
/>
</menu>
For this I am using onOptionsItemSelected function but this function is never called.
It is not supposed to be called. It is not called for any action provider.
I want to get the option which user selects from Share option, so that I can share different text for facebook, messages, mail etc.
That is not directly supported by ShareActionProvider.

Adding a "share" button to share the app on social networks

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

How to remove Messenger Icon from Action Bar

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.

Categories

Resources