Custom sharing dialog android with click callback

In every application we need to add sharing feature for text or image extra. Developer allow user to share content on third party app using Intent. To achieve it we use simple code for sharing text i.e.


Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");

sendIntent.setType("text/plain"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.share_offer_with)));

but from this code we can not know that which application being clicked or we can not share different text or content for particular apps . This blog will help to get callback when user click any app.
Step 1: To get all application which support sharing with below code 

public void shareDialog(){ final List<String> packages=new ArrayList<String>(); Intent shareIntent=new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("text/plain");  
 final List<ResolveInfo> resInfosNew=new ArrayList<ResolveInfo>();  
   
 final List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);resInfosNew.addAll(resInfos);  
 if(!resInfos.isEmpty())  
  {   
 System.out.println("Have package");  
  int count=0;  
  for (ResolveInfo resInfo : resInfos) {   
 String packageName=resInfo.activityInfo.packageName; if(packageName.contains("com.facebook.katana"))  
 { resInfosNew.remove(count); resInfosNew.addAll(resInfos);  
  if(!resInfos.isEmpty()) { System.out.println("Have package");  
  int count=0;  
  for (ResolveInfo resInfo : resInfos)  
  { String packageName=resInfo.activityInfo.packageName; if(packageName.contains("com.facebook.katana")  
 ){ resInfosNew.remove(count); }  
 else packages.add(packageName); count++; } }  
  if (packages.size() > 1) {  
  ArrayAdapter<String> adapter = new ChooserArrayAdapter(this, android.R.layout.select_dialog_item, android.R.id.text1, packages); new AlertDialog.Builder(this) .setTitle(R.string.share_offer_with) .setAdapter(adapter, new DialogInterface.OnClickListener() {  
  public void onClick(DialogInterface dialog, int item ) { invokeApplication(packages.get(item),resInfosNew.get(item)); } }) .show();  
  } else if (packages.size() == 1)  
  { invokeApplication(packages.get(0), resInfos.get(0)); } } }  
 else {  packages.add(packageName);} count++; } }  
  if (packages.size() > 1) {  
  ArrayAdapter<String> adapter = new ChooserArrayAdapter(this, android.R.layout.select_dialog_item, android.R.id.text1, packages); new AlertDialog.Builder(this) .setTitle(R.string.share_offer_with) .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item ) { invokeApplication(packages.get(item),resInfosNew.get(item)); } }) .show(); }   
 else if (packages.size() == 1)  
 { invokeApplication(packages.get(0), resInfos.get(0)); } }  


Step 2 :invokeApplication : Share content on selected app:

 private void invokeApplication(String packageName, ResolveInfo resolveInfo) { Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, resolveInfo.activityInfo.name)); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain");  
  intent.putExtra(Intent.EXTRA_TEXT, "Hi guys, I found amazing thing to share. Send your love and care in form of gifts to your loved ones from anywhere in world. Log on to giftjaipur.com or download app" + "https://goo.gl/YslIVT" +"and use coupon code app50 to get Rs 50 off on your first purchase.");  
  intent.putExtra(Intent.EXTRA_SUBJECT, "GiftJaipur 50 Rs Coupon..."); intent.setPackage(packageName);  
  startActivity(intent); }  


Share Intent
Custom share dialog with callback

Step 2 : Now write ChooserArrayAdapter class for making list of
application with their names and icons


 public class ChooserArrayAdapter extends ArrayAdapter<String>  
  { PackageManager mPm; int mTextViewResourceId; List<String> mPackages; public ChooserArrayAdapter(Context context, int resource, int textViewResourceId, List<String> packages) { super(context, resource, textViewResourceId, packages); mPm = context.getPackageManager(); mTextViewResourceId = textViewResourceId; mPackages = packages; } @Override public View getView(int position, View convertView, ViewGroup parent) { String pkg = mPackages.get(position); View view = super.getView(position, convertView, parent); try { ApplicationInfo ai = mPm.getApplicationInfo(pkg, 0);   
 CharSequence appName = mPm.getApplicationLabel(ai); Drawable appIcon = mPm.getApplicationIcon(pkg);  
  TextView textView = (TextView) view.findViewById(mTextViewResourceId); textView.setText(appName); textView.setCompoundDrawablesWithIntrinsicBounds(appIcon, null, null, null); textView.setCompoundDrawablePadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics())); } catch (NameNotFoundException e) { e.printStackTrace(); }  
  return view; } } 






list adaper
ChooserArrayAdaper.java file look like
Now run application and see the result. You can also custom view according to your requirement.
resultant dialog after implementing this code . 

Download this SampleCode .  and comment below your problem . Please let me any issue.

Previous
Next Post »

3 comments

Click here for comments
Anonymous
admin
18 October 2016 at 09:48 ×

it is help full

Reply
avatar
Unknown
admin
2 March 2018 at 08:44 ×

Actually the code is really bad...

Reply
avatar