Skip to content

Instantly share code, notes, and snippets.

@deep15187
Last active July 19, 2023 00:39
Show Gist options
  • Save deep15187/6592b6e4b4b6bf9b6ea14fb48326ec38 to your computer and use it in GitHub Desktop.
Save deep15187/6592b6e4b4b6bf9b6ea14fb48326ec38 to your computer and use it in GitHub Desktop.
Direct intent to Facebook, Google plus, Linkedin, Twitter, Pinterest, Instagram, Snapchat and Youtube from a fragment after checking if app installed on device
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by deep15187 on 6/22/2016.
*/
public class FragFollow extends Fragment implements View.OnClickListener {
LinearLayout layer_fb;
LinearLayout layer_gp;
LinearLayout layer_ln;
LinearLayout layer_tw;
LinearLayout layer_pnt;
LinearLayout layer_int;
LinearLayout layer_sn;
LinearLayout layer_yt;
TextView txWeb;
CardView cardFollow;
private Context liContext=null;
String urlWeb="{website_url}";
String appFb="fb://page/{fb_page_numerical_id}";
String urlFb="https://www.facebook.com/{fb_page_name}";
String appGp="{google_plus_page_numerical_id}";
String urlGp="https://plus.google.com/{google_plus_page_numerical_id}";
String urlLn="https://www.linkedin.com/company-beta/{linkedin_page_numerical_id}/";
String urlTw="https://twitter.com/{twitter_page_name}";
String urlPt="https://www.pinterest.com/{pinterest_page_name}/";
String appIs="http://instagram.com/{instagram_page_name}";
String urlIs="http://instagram.com/_u/{instagram_page_name}";
String urlSn="https://snapchat.com/add/{snapchat_page_name}";
String urlYt="https://www.youtube.com/channel/{youtube_channel_id}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_follow, container, false);
liContext=this.getActivity();
txWeb = (TextView) view.findViewById(R.id.txtWeb);
layer_fb = (LinearLayout) view.findViewById(R.id.layer_fb);
layer_gp = (LinearLayout) view.findViewById(R.id.layer_gp);
layer_ln = (LinearLayout) view.findViewById(R.id.layer_ln);
layer_tw = (LinearLayout) view.findViewById(R.id.layer_tw);
layer_pnt = (LinearLayout) view.findViewById(R.id.layer_pt);
layer_int = (LinearLayout) view.findViewById(R.id.layer_int);
layer_sn = (LinearLayout) view.findViewById(R.id.layer_sn);
layer_yt = (LinearLayout) view.findViewById(R.id.layer_yt);
cardFollow = (CardView) view.findViewById(R.id.cardFollow);
txWeb.setOnClickListener(this);
layer_fb.setOnClickListener(this);
layer_gp.setOnClickListener(this);
layer_ln.setOnClickListener(this);
layer_tw.setOnClickListener(this);
layer_pnt.setOnClickListener(this);
layer_int.setOnClickListener(this);
layer_sn.setOnClickListener(this);
layer_yt.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.txtWeb:
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlWeb)));
}catch (Exception e){e.printStackTrace();}
break;
case R.id.layer_fb:
try {
if (isAppInstalled(liContext, "com.facebook.orca") || isAppInstalled(liContext, "com.facebook.katana")
|| isAppInstalled(liContext, "com.example.facebook") || isAppInstalled(liContext, "com.facebook.android")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(appFb)));
} else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlFb)));
}
}catch (Exception e){e.printStackTrace();}
break;
case R.id.layer_gp:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.google.android.apps.plus",
"com.google.android.apps.plus.phone.UrlGatewayActivity");
intent.putExtra("customAppUri", appGp);
startActivity(intent);
} catch(ActivityNotFoundException e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlGp)));
}
break;
case R.id.layer_ln:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlLn));
intent.setPackage("com.linkedin.android");
startActivity(intent);
}
catch (ActivityNotFoundException anfe)
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(urlLn)));
}
/*
if(isAppInstalled(getApplicationContext(), "com.linkedin.android")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlLn)));
}else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlLn)));
}*/
break;
case R.id.layer_tw:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlTw));
intent.setPackage("com.twitter.android");
startActivity(intent);
}
catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(urlTw)));
}
/*
if(isAppInstalled(getApplicationContext(), "com.twitter.android")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlTw)));
}else {
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse(urlTw));
startActivity(i);
}*/
break;
case R.id.layer_pt:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlPt));
intent.setPackage("com.pinterest");
startActivity(intent);
}
catch (ActivityNotFoundException anfe)
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(urlPt)));
}
/*
if(isAppInstalled(getApplicationContext(), "com.pinterest")){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPt)));
}else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPt)));
}*/
break;
case R.id.layer_int:
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(appIs));
intent.setPackage("com.instagram.android");
startActivity(intent);
}
catch (ActivityNotFoundException anfe)
{
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(urlIs)));
}
/*
if(isAppInstalled(getApplicationContext(), "com.instagram.android")){
Toast.makeText(ActivityFollow.this, "Instagram", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(appIs)));
}else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlIs)));
}*/
break;
case R.id.layer_sn:
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.snapchat.android");
startActivity(Intent.createChooser(intent, "Open Snapchat"));
}
catch (ActivityNotFoundException anfe)
{
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlSn)));
}
break;
case R.id.layer_yt:
try {
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setPackage("com.google.android.youtube");
intent.setData(Uri.parse(urlYt));
startActivity(intent);
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlYt)));
}
break;
default:
break;
}
}
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment