Share Url Links and Texts into Social Networks (Facebook,Twitter and linkedin)

Introduction

     This post will help to share your urls and text values into social networks like facebook,twitter and linkedin.in facebook we have to share your urls only, but twitter and linkedin able to share both urls and text.

demo







Using the code

activity_main.xml:


<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share Urls and Text Values"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:textColor="#FF0000"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/textView1">

<ImageView
android:id="@+id/fb_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:src="@drawable/fb" />

<ImageView
android:id="@+id/twitter_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@+id/fb_icon"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:src="@drawable/twitter" />

<ImageView
android:id="@+id/linkedin_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/linkedin"
android:layout_below="@+id/twitter_icon"
android:layout_centerInParent="true"
android:layout_marginTop="15dp" />

</RelativeLayout>


</RelativeLayout>


MainActivity.java:
package com.example.social_share;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
ImageView facebook,twitter,linkedin;
Intent twitterIntent,linkedinIntent,facebookIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

facebook=(ImageView)findViewById(R.id.fb_icon);
twitter=(ImageView)findViewById(R.id.twitter_icon);
linkedin=(ImageView)findViewById(R.id.linkedin_icon);
facebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String urlToShare = "http://kalisandroid.blogspot.com/";
facebookIntent = new Intent(Intent.ACTION_SEND);
facebookIntent.setType("text/plain");
facebookIntent.putExtra(Intent.EXTRA_TEXT, urlToShare);

boolean facebookAppFound = false;
List<ResolveInfo> matches = getPackageManager()
.queryIntentActivities(facebookIntent, 0);

for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.toLowerCase().startsWith(
"com.facebook")) {
facebookIntent.setPackage(info.activityInfo.packageName);
facebookAppFound = true;
break;
}
}

if (facebookAppFound) {
startActivity(facebookIntent);
}
else
{
Toast.makeText(MainActivity.this,"Facebook app not Insatlled in your mobile"4).show();
}

}
});
twitter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String text = "...kaLis...";
twitterIntent = new Intent(Intent.ACTION_SEND);
twitterIntent.setType("text/plain");
twitterIntent.putExtra(Intent.EXTRA_TEXT, text);

boolean twitterAppFound = false;
List<ResolveInfo> matches1 = getPackageManager()
.queryIntentActivities(twitterIntent, 0);
for (ResolveInfo info : matches1) {
if (info.activityInfo.packageName.toLowerCase().startsWith(
"com.twitter")) {
twitterIntent.setPackage(info.activityInfo.packageName);
twitterAppFound = true;
break;
}
}

if (twitterAppFound) {
startActivity(twitterIntent);
}
else
{
Toast.makeText(MainActivity.this,"Twitter app not Insatlled in your mobile"4).show();
}
}
});
linkedin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String text1 = "...kaLis...";
linkedinIntent = new Intent(Intent.ACTION_SEND);
linkedinIntent.setType("text/plain");
linkedinIntent.putExtra(Intent.EXTRA_TEXT, text1);

boolean linkedinAppFound = false;
List<ResolveInfo> matches2 = getPackageManager()
.queryIntentActivities(linkedinIntent, 0);

for (ResolveInfo info : matches2) {
if (info.activityInfo.packageName.toLowerCase().startsWith(
"com.linkedin")) {
linkedinIntent.setPackage(info.activityInfo.packageName);
linkedinAppFound = true;
break;
}
}

if (linkedinAppFound) {
startActivity(linkedinIntent);

}
else
{
Toast.makeText(MainActivity.this,"LinkedIn app not Insatlled in your mobile"4).show();
}
}
});

}

}

                                                  
Previous
Next Post »

1 comments:

Write comments
Alpesh
AUTHOR
August 17, 2016 at 3:58 PM delete

its good idea to share url in social network

Reply
avatar