How to share text on whatsapp in android? - android

I am using a button in a listview and want to share text on whatsapp...here is my code...this code is working without taking a listview...but i making a project in which i am using a listview.....
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Try this : https://play.google.com/store/apps/details?id=com.listviewexample");
startActivity(Intent.createChooser(intent, "Share via"));

Try this
public void onClickWhatsApp(View view) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Try this : https://play.google.com/store/apps/details?id=com.listviewexample";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
}

Related

Android: Intent createChooser not working properly

I am adding share feature in the app. Below is my code:
public static void shareData(String title, String message, Uri imageUri, Activity activity) {
try {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TITLE, title);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, message);
if (imageUri != null) {
Logger.errorMessage(ShareUtil.class.getSimpleName(), "share if");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
sendIntent.setType("image/*");
} else {
Logger.errorMessage(ShareUtil.class.getSimpleName(), "share else");
sendIntent.setType("text/plain");
}
activity.startActivity(Intent.createChooser(sendIntent, "Share using"));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(activity, "No App found to share", Toast.LENGTH_LONG).show();
}
}
This is showing the list of apps, but when I select any app, the title and message is not sent. I am not getting what I am doing wrong.
Intent.EXTRA_SUBJECT is mainly used in email. Try replacing it with Intent.EXTRA_TEXT. This works in most apps.

Mms in android showing "You can't add this picture to your message"

There are other questions in MMs but none have solved my issue and i simply don't know why!!
String fileName = "/one.png";
File dataFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath());
Toast.makeText(c, String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()), Toast.LENGTH_LONG).show();
if(dataFile.exists())
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+fileName));
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.getApplicationContext().startActivity(new_intent);
}
else
{
Toast.makeText(c, "Not Found", Toast.LENGTH_LONG).show();
}
what is i am doing wrong!!

Sending Msg to Whatsapp using Number from SQLite

Is it Possible to send whatsapp from the number stored in database(SQLite)
SO Far I have tried sending a whatsapp from older post but its sending ony msgs or opening only contacts when i am combining both I am not getting any result
//This Will Open Specific Contact on Whatsapp
void openWhatsappContact() {
String number = "919033152265";
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
}
//This Will Send Specific Msg to WhatsAPP
public void SelectContactAndMsgSend(View view) {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
//Combining Both Not getting Any result
public void CombiningBoth(View view) {
String number = "Here GOes My Number"; //As i am from india my number Im putting is 91xxxxxxxxxx
Uri uri = Uri.parse("smsto:" + number);
Intent waIntent = new Intent(Intent.ACTION_SENDTO, uri);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, ""));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}

How to Share Image And Text to Facebook and Twitter From Android

I am developing one application in that I want to share Image and some text. Both are send to twitter but while I share to facebook image only share but not shared text.
My code is:
public void open(View view) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/photo.jpg");
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
sharingIntent.putExtra(Intent.EXTRA_TITLE, "Traffic At");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
public class Share extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent i=getIntent();
String data=i.getStringExtra("data");
String shareType=i.getStringExtra("type");
if(shareType.equals("text"))
{
sharetext(data);
}
else if(shareType.equals("image"))
{
shareimage(data);
}
}
public void sharetext(String text) //Text to be shared
{
Intent share=new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_SUBJECT,"TITLE");
share.putExtra(android.content.Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(share,"Share via"));
finish();
}
public void shareimage(String text) //argument is image file name with extention
{
Intent shareimage=new Intent(android.content.Intent.ACTION_SEND);
shareimage.setType("*/*");//for all
shareimage.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
shareimage.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+text));
startActivity(Intent.createChooser(shareimage, "Share Image"));
finish();
}
}
call this activity
with bundle containing values
StringExtra("data"); //file name or text to share
StringExtra("type"); // "text" for sharing text, "image" for sharing image
Try to use the following method.
void share(String appName, String title, String text, String imagePath) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/*"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(appName) || info.activityInfo.name.toLowerCase().contains(appName)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, title);
targetedShare.putExtra(Intent.EXTRA_TEXT, text);
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM", "Exception while sending image on" + nameApp + " " + e.getMessage());
}
}
To share via facebook call share("facebook", title, text, imagePath), and share("twitter", title, text, imagePath) for twitter.

How to use whatsapp from my Android app?

This is how I am calling the SMS app:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "The SMS text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
How do I do the same for sending messages via twitter/Whatsapp/Facebook? What should I write in place of mms-sms? I found no documentation on such.
I can't also find any way of calling Facebook/Twitter directly, but you could always call android.content.Intent.ACTION_SEND and let the user choose the application.
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "Message body");
startActivity(Intent.createChooser(i, "Share dialog title"));
However, there might be a bug when using this to share through Facebook. For more information please see: Android Facebook Intent
public void onClickWhatsApp(View view) {
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.putExtra("chat",true);
i.setType("text/plain");
startActivity(i);
You can use the following snippets:
For WhatsApp:
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
For Twitter:
void shareOnTwitter()
{
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Insert Tweet Here";
#SuppressWarnings("unused")
PackageInfo info=pm.getPackageInfo("com.twitter.android", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.twitter.android");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT)
.show();
return ;
}
return ;
}

Categories

Resources