Show share menu like other apps on my android phone - android

I have added share action on my app's actionbar and followed these steps:
http://www.codewithasp.net/2016/11/share-action-provider-android-application-actionbar-appcompat-v7.html
This is showing a nice simple looking share menu on my actionbar. But problem is that all other application on my phone have different share menu and all of them are similar.
Here is how my share menu look:
Here is how other apps showing share menu on my device

Instead of creating a drop-down menu with share options, you should just call share intent once you've clicked on share button or menu option. That way the list of possible apps would be shown as on the example you've pasted.
Here is an example of how could you do it.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
sendIntent.setType("text/plain");
getContext().startActivity(sendIntent);

There was one more thing to add in SadClown's answer and I got what I wanted. Actually while calling startActivity instead of just pass your share intent we need to call intent chooser
getContext().startActivity(Intent.createChooser(sendIntent, "Share"));
It is explained here in detail

Related

Android share action. Trigger from navigation drawer rather than top bar.

My app has a navigation drawer and as one of its options on the left side I would like it to have "Share with friends". User can tap on Share with friends menu option and will trigger Android native Share action. Is it possible? Can you share with me a link on Tutorial or code sample on how to trigger Android Share action from other places like left side menu for example rather than top bar.
Thank you!
You can do this:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "message to share");
startActivity(Intent.createChooser(share, "title"));

Whatsapp error Audio/Sound setResult intent

I have an app that can send a normal intent by calling startActivity with:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("audio/*");
sharingIntent.setPackage("com.whatsapp");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(filepath));
startActivity(sharingIntent);
But i want my app on whatsapp attach menu (Audio Item specifically) like this related discussion example: Returning an Image to whatsapp
So, i used the code below, my app is showed on menu but when i do the share steps i get whatsapp "Share failed, please try again" error message. Others similar apps do the steps with the final step showing the Whatsapp Recorder dialog to apply sound on chat.
On this second feature (whatsapp internal intent-filter sharing) i use the same intent but with this:
setResult(RESULT_OK, buildSoundShareIntent(soundId));
finish();
instead of startActivity
Is there something specific and hidden that i dont find?
The solution is below my noose all the time, its simple: call the Intent constructor with action and data (Uri.fromFile(File)):
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND, Uri.fromFile(file));
setResult(sharingIntent);
In camera Settings; set storage to "Ext. SD card". Also, move any older photos you want to share to Ext. SD card. This worked perfectly with me. Hope it will work for you.

In share option in dialog box, I want to add my custom intent option

I am using this code to share link in android app.
Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject test");
i.putExtra(android.content.Intent.EXTRA_TEXT, "extra text that you want to put");
startActivity(Intent.createChooser(i,"Share via"));
But i want to add cutom intent option in dialoag box.
If i have twitter and facebook app in my phone, then above code give me share option
1) Twitter
2) Facebook
3) Bluetooth(if phn supported)
Also with this I want to add
4) Commnets (on comments click user will go my Comment activity)
I want to make share like this.
Can anyone help. Thanks in advance.
You are creating a chooser. It contains all intents that reply to ACTION_SEND text/plain.
To have your comment activity in that list, you need make an intent-filter in your manifest matching that actions for your comment Activity.
Alternatively (and moreeasily, too), if you don't want your Comment activity to respond to external ACTION_SEND text/plain, you can use the response given to this question : Custom filtering of intent chooser based on installed Android package name : Add a Intent.EXTRA_INITIAL_INTENTS extra to the intent.

What is createChooser when I have to use Intents? What I could do with this method?

I have been taking a look over stackoverflow but I did't find a definition about what is "createChooser" and why I can use and in whick kind of situations is good to use it.
Thanks in advance.
For example: you have a share picture option in your application.
You define an intent like this:
Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/jpeg");
File downloadedPic = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS),
"q.jpeg");
picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));
Than when you call:
startActivity(picMessageIntent);
all applications on your phone capable of getting this picture will be listed.
If you want to custimize the title of that list, you can use createChooser like this:
startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));
When startActivity(intent) is called for the first time from your app the user sees a list of all apps capable to handle this intent.
There is also an option to always handle this intent using one of the apps from the list. If this option is used then the list will never be shown again.
If you use createChooser in your intent then the "always use this app" option is not shown. The user always sees this list.
This method is used when you want to create a Custom Action using an Intent... Just like what android provides ACTION_VIEW etc... but here when there are multiple choices to perform an an Action this chooser will bring up a dialog which will have all available options and let the user select one... here is an example

Filtering ACTION_SEND sharing options

I am using the following code to share information from my application.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "share Content" );
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "share subject" );
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, "Share Place via"));
The above code displays some options through which i can share, What i would like to achieve is somehow go through the options and omit some of them.
For Example , in my case, if Facebook app is present on the device, the above code displays it as one of the options. As i already have Facebook android sdk integrated in my application. I want to remove the Facebook option from the sharing options.
I think you should have a look at the PackageManager queryIntentActivities (Intent intent, int flags) methods. This will give you a list of Activities matching your Intentand then you can maybe remove some of them, and present a custom Dialog to the user, where you show just the desired Activities. And after the user chooses an Activity you'll have to explicitly start that Activity.

Categories

Resources