How to share text in twitter,facebook and email in android? - android

I am working on android application. My app contains a listview and when each listitem is clicked I am displaying its
related content in new activity. Now I kept a share button in the new activity and when that button is clicked I am displaying an alert with
fb,twitter and email icon. Now when I click on twitter the text in that page should be posted in twitter. Similarly for facebook and email. Please suggest me
how to share text via social networking in android. I didnt work on sharing via social network android. Any suggestion or links may be helpful.
I have gone through examples in google but I didnt understood the flow.

use this and let user choose where he/she wants to share it
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Hello");
i.putExtra(Intent.EXTRA_TEXT, "I am sharing this");
try
{
this.startActivity(Intent.createChooser(i, "Share..."));
}
catch (android.content.ActivityNotFoundException ex)
{
//Error
return;
}

String message = "Text you want to share.";
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(shareIntent, your title here));

Related

Android - Chooser dialog for sharing -- different intents depending on application selected?

I'd like to share different information depending on the application selected by the user. For example, I'd like to send shorter text to Twitter than I do Gmail (otherwise, I go over in characters). Is there anyway to achieve this? Bonus question, is it possible to make hyperlinks via sharing via Gmail or SMS. For example, "Download here" where 'here' is a link instead of "Download by clicking the link below: http://..."
Current code:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
startActivity(Intent.createChooser(shareIntent, "Share this story"));
To find the selected share target this could be interesting for you: Branching the Android Share Intent extras depending on which method they choose to share
By using below code may helps you:
String shareText="Download <a href='[download link]'>here</a>";
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.toHtml(shareText));
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
startActivity(Intent.createChooser(shareIntent, "Share this story"));
Share Url Links and Texts into Social Networks (Facebook,Twitter and linkedin)

How to set different share in the same shareButton

I am stuck in my app trying to set a share button.
I have a simple textview to be shared with a few of social networks.
As i found out i cannot share just text with Facebook without using Facebook SDK.
Now i d like to know if is possibile to set a thing like. If user press whatsapp share my string if user press Facebook share the link of my app.
Below is my share button code.
case R.id.action_share:
Intent intent2=new Intent(Intent.ACTION_SEND);
intent2.setType("text/plain");
intent2.putExtra(Intent.EXTRA_TEXT, random + "\n"+"By Random Quotes" ); //random is the textview
startActivity(Intent.createChooser(intent2, "Share via"));
break;
you have to start activity with new task,like this:-
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.putExtra(Intent.EXTRA_SUBJECT, "share discription");
share.putExtra(Intent.EXTRA_TEXT,"text you want to share");
startActivity(Intent.createChooser(share,"you share title"));
}

add only specific apps to shareactionprovider

ShareActionProvider provides share for every compatible app on the device. I would like to specify which apps I want to allow sharing, like only twitter and gmail for example.
How would I do this?
Google don't recommend that you share to specific apps as there are various different Twitter clients - it's better to just let people chose where they want to share it.
However, the following code will send the message "hello twitter" to twitter on a button press.
String message = "hello Twitter";
try {
//try to open the official twitter app
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT, "Subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
sharingIntent.setPackage("com.twitter.android");
startActivity(sharingIntent);
} catch (Exception e) {
//fallback on opening an internet browser
Log.e("Danielle", "exception=" + e.toString());
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri
.parse("https://mobile.twitter.com/compose/tweet"));
startActivity(i);
}
Hope that helps.
If you are ok with just using a popup, the answer by dacoinminster in this question How to filter specific apps for ACTION_SEND intent (and set a different text for each app) will do it.
But if you specifically want it to be an actionprovider dropdown, I have not find this solution either.

Android Intent ACTION.SEND -- can't send text to Twitter or Facebook

I am having trouble when I try to pass a text string from within an EditText to either Facebook or Twitter via an intent. In fact, the only option which works currently is email.
The code I am using is below:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, textVariable.getText());
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject Title");
startActivity(Intent.createChooser(intent, "Share Text"));
I have tried it with and without the subject line. Any thoughts?
Thank you :)
To share the text on Facebook and Twitter the way you are using is not correct. You need to try out the other way of sharing.
Just refer Link it will guide you on how to share the text on facebook & twitter using intent.
The following code should do the trick!
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.twitter.android",
"com.twitter.applib.composer.TextFirstComposerActivity"));
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(intent);
// Success!
} catch (Exception e) {
// official twitter code missing
}
Look Share bitmap with text on Twitter, Email & bitmap on Facebook in Android using Intent
You can share image with text caption on twitter but on facebook you can not share text caption using Intent, for that you have to use Facebook SDK.

android - Share html link to facebook, twitter or email

I am trying to share an html link to either facebook, twitter or email. Here is what I have so far, but two things are going wrong.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(Intent.EXTRA_TEXT, "<!DOCTYPE html><html><body>" + htmlUrl + "</body></html>");
startActivity(Intent.createChooser(shareIntent, "Share!"));
WHERE htmlUrl = "<a href=\"http://{url}/?q=" + queryString.substring(0, queryString.length() - 1) + "\" >Text to url! </a>"
First this only shows the email application in the list.
Secondly it's showing up as full text within the email and not as an HTML item.
Thank,
Dman
Your MIME type is wrong. Use this instead:
shareIntent.setType("text/plain");
If you just want to share a link and not full-blown HTML, just use the url as the Intent.EXTRA_TEXT value:
shareIntent.putExtra( Intent.EXTRA_TEXT, url );
Note that only a few apps (like GMail, Bluetooth and Dropbox) support sharing HTML. Use plain text to allow more apps to catch your Intent
You only miss a small change to your code - use Html.fromHtml to encode the string as HTML.
a more detailed example is available at this link:
http://blog.iangclifton.com/2010/05/17/sending-html-email-with-android-intent/
This will not allow you to share on Facebook since the Facebook app dows not support ACTION_SEND with text/html but this will allow you to share HTML content using the Gmail app
Share to Facebook (using this method can only share the link)
Intent facebookIntent = new Intent(Intent.ACTION_SEND);
facebookIntent.setType("text/plain");
facebookIntent.setPackage("com.facebook.katana");
facebookIntent.putExtra(Intent.EXTRA_TEXT, shareUrl);
startActivity(facebookIntent);
Share to Email (using this method can change the email content to html format, but don't work using default email client, it works on gmail client.)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_SUBJECT, shareTitle);
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(content of your email));
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Share to Email..."));
Share to Twitter
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, your content);
intent.setType("application/twitter");
startActivity(intent);
Hope this helps.
This
shareIntent.setType("text/html");
should do for the html part. Why only the email shows up no idea, do you have other apps like facebook, twitter etc installed to handle share intents?

Categories

Resources