Share this application with your friends on settings screen via social network

In this blog post, i would show you how to code to share this application with your friends option on settings screen via social network.

To achieve this, you need to know how to code settings screen using preference manager, i have explained this in my previous blog.

This blog is an additional explanation content of my previous blog and to complete the settings screen.

So here we go.

First create a button on xml i.e user_setting.xml at xml folder under res folder.


<Preference android:title="Share with friends"
android:key="buttonshare"
android:summary="links only and it will redirect to other app's"/>

Now on your MainActivity.java class, create an .setOnPreferenceClickListener for the above Button.


Preference button_Share = (Preference) findPreference("buttonshare");

button_Share.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {

return true;
}
});

Now create an AlertDialog with ListView using ArrayAdapter for OnPreferenceClick of the above Button.


ArrayAdapter<String> adapter;

String[] countries = {"Email", "Facebook", "Twitter", "LinkendIn"};

adapter = new ArrayAdapter<String>(cont, R.layout.list_item, countries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

button_Share.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
// code to create an alertdialog
new AlertDialog.Builder(cont)
.setTitle("Share via")
.setAdapter(adapter, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
}).create().show();

return true;
}
});

In the above code, i’m allowing user to sharing this application via Email or Facebook or Twitter or LinkedIn.

Now, inside the onClick of the dialog, i’m using switch case for user choice.


switch(which){ //which is the selected Item No on the Array list
case 0:   //for via Email choice

break;

case 1:    // for via Facebook choice

break;

case 2:    // for via Twitter choice

break;

case 3:    //for via LinkedIn choice

break;

}

Case 0: // code for Email choice

String Url = "http://play.google.com/store/apps/details?id=" + cont.getPackageName();
final String appPackageName = cont.getPackageName(); //getPackageName() from Context or Activity
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ }); // empty string array because we don't know for who user will send
email.putExtra(Intent.EXTRA_SUBJECT, "The Android App, (Your App Name) has been recommended for you."); // subject
email.putExtra(Intent.EXTRA_TEXT, "I have been using (Your App Name) and i think you might like it. Check out from your Android Phone: https://market://details?id=" + appPackageName +"\n or\n"+Url+"\n\n Sent from my Android Phone."); //sending application link to the user
email.setType("message/rfc822"); // we need setType to prompts only
email clients. startActivity(Intent.createChooser(email, "Choose an Email client :"));

Case 1: // code for Facebook choice

String Url = "http://play.google.com/store/apps/details?id=" + cont.getPackageName();
Intent facebookIntent = new Intent(Intent.ACTION_SEND);
 facebookIntent.setType("text/plain");
 facebookIntent.putExtra(Intent.EXTRA_TEXT, Url);

 boolean facebookAppFound = false;
 List<ResolveInfo> matches = cont.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(cont,"Facebook app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }

Case 2: // code for Twitter choice

Intent twitterIntent = new Intent(Intent.ACTION_SEND);
 twitterIntent.setType("text/plain");
 twitterIntent.putExtra(Intent.EXTRA_TEXT, Url);

 boolean twitterAppFound = false;
 List<ResolveInfo> matches1 = cont.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(cont,"Twitter app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }

Case 3: // code for LinkedIn choice

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

 boolean linkedinAppFound = false;
 List<ResolveInfo> matches2 = cont.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(cont,"LinkedIn app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }

Complete code:
MainActivity.java

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 // Display the fragment as the main content.
 if (savedInstanceState == null)
 getFragmentManager().beginTransaction()
 .add(android.R.id.content, new PrefFragment()).commit();
 }

 public static class PrefFragment extends PreferenceFragment implements
 OnSharedPreferenceChangeListener {
 Context cont;
 Preference button_Share;
 ArrayAdapter<String> adapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 cont = getActivity();
 addPreferencesFromResource(R.xml.user_setting);

 button_Share = (Preference) findPreference("buttonshare");

 String[] countries = {"Email", "Facebook", "Twitter", "Linkendin"};

 adapter = new ArrayAdapter<String>(cont, R.layout.list_item, countries);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 button_Share
 .setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
 @Override
 public boolean onPreferenceClick(Preference arg0) {
 // code for what you want it to do
 new AlertDialog.Builder(cont)
 .setTitle("Share via")
 .setAdapter(adapter, new DialogInterface.OnClickListener() {

 @Override
 public void onClick(DialogInterface dialog, int which) {

 // TODO: user specific action
 String Url = "http://play.google.com/store/apps/details?id=" + cont.getPackageName();
 switch(which){
 case 0:
 final String appPackageName = cont.getPackageName();
 Intent email = new Intent(Intent.ACTION_SEND);
 email.putExtra(Intent.EXTRA_EMAIL, new String[]{ });
 email.putExtra(Intent.EXTRA_SUBJECT, "The Android App, (Your App Name) has been recommended for you.");
 email.putExtra(Intent.EXTRA_TEXT, "I have been using (Your App Name) and i think you might like it. Check out from your Android Phone: https://market://details?id=" + appPackageName +"\n or\n"+Url+"\n\n Sent from my Android Phone.");

 // we need setType to prompts only email clients.
 email.setType("message/rfc822");

 startActivity(Intent.createChooser(email, "Choose an Email client :"));
 break;

 case 1:
 Intent facebookIntent = new Intent(Intent.ACTION_SEND);
 facebookIntent.setType("text/plain");
 facebookIntent.putExtra(Intent.EXTRA_TEXT, Url);

 boolean facebookAppFound = false;
 List<ResolveInfo> matches = cont.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(cont,"Facebook app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }
 break;
 case 2:
 Intent twitterIntent = new Intent(Intent.ACTION_SEND);
 twitterIntent.setType("text/plain");
 twitterIntent.putExtra(Intent.EXTRA_TEXT, Url);

 boolean twitterAppFound = false;
 List<ResolveInfo> matches1 = cont.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(cont,"Twitter app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }
 break;
 case 3:
 Intent linkedinIntent = new Intent(Intent.ACTION_SEND);
 linkedinIntent.setType("text/plain");
 linkedinIntent.putExtra(Intent.EXTRA_TEXT, Url);

 boolean linkedinAppFound = false;
 List<ResolveInfo> matches2 = cont.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(cont,"LinkedIn app not Insatlled in your mobile", Toast.LENGTH_SHORT).show();
 }
 break;
 }

 dialog.dismiss();
 }
 }).create().show();

 return true;
 }
 });

 }

 @Override
 public void onSharedPreferenceChanged(SharedPreferences arg0,
 String arg1) {
 // TODO Auto-generated method stub

 }
 }
}

user_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
 android:title="Settings">

 <PreferenceCategory
 android:title="GENERAL">

 <Preference android:title="Share with friends"
 android:key="buttonshare"
 android:summary="links only and it will redirect to other app's"/>

 </PreferenceCategory>

</PreferenceScreen>

Screenshots:
Screenshot_2014-10-06-13-22-17[1] Screenshot_2014-10-06-13-22-28[1] Screenshot_2014-10-06-13-23-30Screenshot_2014-10-06-13-23-54[1] Screenshot_2014-10-06-13-36-19[1]

Tips to Remember to always:

1) In Facebook, we can share only URL’s where as in Twitter and LinkedIn, we can share both text and URL’s.

2) Facebook, Twitter and LinkedIn has the ability to grab the thumbnail or the feature image from the source and provide us a preview of our app(if published).

3) In this demo, we are using share option only with the installed application, rather than redirecting to browser if app is not available.

4) In this demo, we are not using any special permission, Its just a normal app only.

 

Download Source Code from here.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

Your email address will not be published. Required fields are marked *