Select Page

Every app has an “About us” section which speaks about the app, the company or the developer.

Every time a user decides to go to the “About Us” page, he or she is on a fact-finding mission, as well as looking for the story of the creator. Most of these apps share their social connections here so that the user gets easy access to connect & communicate with them thus building the user feedback.

After working on the same for 10+ times I thought why not this be done with a single line of code. So here it is, my post talking about how you can connect social media account in a single line of code.

Yes, you read it right! a single line of code.

Social accounts: Facebook, Twitter, LinkedIn, Instagram, PlayStore, GitHub, YouTube

You can send emails & also load any valid URL with this.

I made a Gist, it’s over on GitHub. Just insert that file into your application and call the necessary function with the needed data.

This is how the Util class works,

  1. Check internet connection,
  2. Check the availability of the application,
  3. If available redirect to the concerned screen, else open the page in the browser.

1. Check Internet Connection:


public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager connectivityManager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();

}

2. Check the availability of the application:


public static Boolean isAppAvailable(Context context, String appName) {
    PackageManager pm = context.getPackageManager();
    boolean isInstalled;
    try {
        pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
        isInstalled = true;
    } catch (PackageManager.NameNotFoundException e) {
        isInstalled = false;
    }
    return isInstalled;
}

3. If app available redirect to the concerned screen else opens the page in browser:


if (isNetworkAvailable(context)) {
    if (isAppAvailable(context, "app package name")) {
        intent.setPackage("app package name");
        intent.setData(Uri.parse("page URI" + id));
    } else
        intent.setData(Uri.parse("URL" + id));


    context.startActivity(intent);
}

AboutUs


SocialConnection :

It is a utility class used to connect social media accounts. All the functions are the static function so need to create an instance, you can directly call the method using the function name along with the class name like


SocialConnection.connectPlayStoreApp(this,"package name");
SocialConnection.loadURL(this,"Valid URL");
SocialConnection.connectTwitter(this,"Twitter Handle");

SocialConnection contains the following methods. For all this method you need to pass the Context and the unique ID of the account as arguments.

  • connectFacebook : connect Facebook account.
  • connectTwitter : connect Twitter handle.
  • connectInstagram : link Instagram id.
  • sendMail : send the e-mail with/without a subject.
  • connectLinkedIn : connect LinkedIn profile .
  • connectYouTube : link to YouTube channel.
  • connectGitHub : connect GitHub profile.
  • connectPlayStoreApp : link an app.
  • listPlayStoreApps : list out all apps of a Developer ID.
  • loadURL : load a URL.
  • setCheckInternetConnection : check internet connection based on the boolean flag.
  • setShowErrorMessage : Toast network error message based on the flag .
  • setNetworkErrorMessage : to set network error message.

 

View Gist
Elsewhere

Jaison Fernando

Android Developer at NFN Labs
Elsewhere