How to show sharing menus within activity? - android

I want to share a text using this:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
As you can see in the below picture, I know how to acheive a dialog which lets user picks his choice (My app). But recently I saw an app which is letting users pick their choice within its activity (Desired).
Any idea how to acheive desired one? Thank you all.

This is the code, I've been using for sharing 'Referral Code' from within my app:
ShareCompat.IntentBuilder
.from(this) // getActivity() or activity field if within Fragment
.setText("message") // This will be populated as user's message
.setType("text/plain") // most general text sharing MIME type
.setChooserTitle("Share code using:")
.startChooser();
It opens up a view similar to your right image.

Create Custom layout. (Right image)
Embed in BottomSheetLayout
Use setState() to show your "Share Screen"
................................................
or You can just do the same inside an AlertDialog. (Left Image)

You can query for the list of apps that are registered to handle the action and then display the result in the way that you like the most. When the user will choose something you can start the selected activity.

Related

Prevent auto choose which sharing application use

I'm develop an application in Android.
I have a ListView with some image, when I click a row I open ContextMenu and I have two Choise, eliminate the image or sharing with other application.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
Uri r = Util.getImageUri(getActivity().getApplication(), image);
intent.putExtra(Intent.EXTRA_STREAM, r);
startActivity(intent);
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
How can I prevent it?
I want that user choose every time sharing application.
Thanks.
t
I use this code to sharing the image, but after the first time, every time that I try to share image, the application choose always the first application with wich I send the first image.
That was because you indicated, to the chooser, that you wanted to make this choice "always", instead of "just once".
I want that user choose every time sharing application
Use:
startActivity(Intent.createChooser(intent, "..."));
where "..." is your explanation for this.
In your phone go to settings-->Apps then select the app which get opens by default then there you will find an option "Open by default" open that option, in there press "Clear Defaults".
Then in your app again you will get the list of application to share with

Detecting the target application when sending an Intent

I am sharing image by using the send intent(ACTION_SEND).
I want to know if any application is selected for sharing or not. How can I do that and how do I know if the image was sent successfully?
Code I have used to share image is here :
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(imageSharePath)));
startActivity(Intent.createChooser(share, "Share Image"));
You need to implement your own dialog for the activity selection.
To create such dialogs you need to use PackageManager.queryIntentActivities(). This method returns List<ResolveInfo>.
ResolveInfo contains some information about an activity (e.g. resolveInfo.activityInfo.packageName), and with the help of PackageManager you can get other information (useful for displaying the activity in the dialog) - application icon drawable, application label, etc.
Display the results in a list in a dialog (or in an activity styled as a dialog). When an item is clicked create new Intent.ACTION_SEND, add the content you want and add the package of the selected activity intent.setPackage(pkgName).
The answer above is no longer correct.
Since API 22 it is possible to detect the target application when the user shares.
For details see:
Get IntentSender object for createChooser method in Android
https://medium.com/code-with-lisa/get-results-from-android-chooser-9cfc5445a871

Show Intent.ACTION_PICK in landscape

I am working on an app which let users choose photo from their gallery. The problem is I need to show the gallery in landscape only. Is it possible to do it?
I use this code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RQC_GALLERY);
When you start any activity using explicit intent, you loose control from your application. Android finds out and application to perform that is described in intent object.
Intent object attributes details
From no where you can have control on next application regarding view.
If you want to fulfill specific requirement than make your own activity with Grid view and use image adapter to set images.
Here is one link which shows how to set images in adapter. It will help to design your code if necessary.

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

Categories

Resources