I need to add a share button to one view inside one of my DialogFragments (Not in the action bar).
Can you give me a clue or a sample maybe? All I could find googling is for action bar (like Sherlok) which is not what I'm looking for.
Step #1: Put a button inside of your DialogFragment.
Step #2: When the user clicks the button, create an ACTION_SEND Intent to share whatever it is you want to share, and call startActivity() on it (or, if desired, wrap the Intent you create in a chooser Intent via Intent.createChooser()):
void sendIt(String theMessage) {
Intent i=new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, R.string.share_subject);
i.putExtra(Intent.EXTRA_TEXT, theMessage);
startActivity(Intent.createChooser(i,
getString(R.string.share_title)));
}
Related
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
With the code below I share some text from my app via an intent chooser. My problem is that if I for example choose to share the text as a message and when in the message app I just hit the home button and bring my app back to front. Then when I try to start a new intent chooser it immediately sends me back to the unfinished message app. I would rather that it forgot all about that and presented me with the list of choices again.
Is there a way to make it forget the old choice?
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(intent, "Share text as..."));
Add the following
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
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.
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
friend's,
I there any possibility to bind the result of Intent class in xml layout,
i'm doing it so for sending mail,i'm using below code for sending mail in
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "info#blacksheep.com" });
sendIntent.setType("vnd.android.cursor.dir/email");
when i use the above code it opens a new page,but i need it to be in the layout alone.
How can i get it.
Thanks in advance.
when i use the above code it opens a new page,but i need it to be in the layout alone. How can i get it.
You cannot do that, sorry. You are opening an activity from another application (whatever the user chooses to handle your ACTION_SEND request) -- it controls the presentation, not you.