3

Code For Opening Sharing Intent

String xtype = "image/*";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(xtype);
Uri uri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider", new File(tmpImageUri.getPath()));
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share via"));

it shows sharing intent dialog like this

Default Share Intent

But now I want it like this

I want Like Sharing Intent

Is that Possible to customise Sharing Intent Dialog ?

piet.t
  • 11,400
  • 21
  • 42
  • 50
Ashvin solanki
  • 4,200
  • 2
  • 18
  • 58

1 Answers1

9

Yes, it is possible to create custom sharing dialog. You just need to get the IntentActivity list and customize as per your requirement. For Sample you can do as below.

Step 1: Prepare Intent

String urlToShare = "https://play.google.com/store/apps/details?id=com.yourapp.packagename";
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
// intent.putExtra(Intent.EXTRA_SUBJECT, "If any extra"); // NB: has no effect!
intent.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application \n\n" + urlToShare);

Step 2: Get activity list and set in ListAdapter.

final List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);

List<DialogItem> appNames = new ArrayList<DialogItem>();

    for (ResolveInfo info : activities) {
            appNames.add(new DialogItem(info.loadLabel(getPackageManager()).toString(),
                    info.loadIcon(getPackageManager())));
    }
final List<DialogItem> newItem = appNames;
ListAdapter adapter = new ArrayAdapter<DialogItem>(activity,
                android.R.layout.select_dialog_item, android.R.id.text1, newItem) {
            public View getView(int position, View convertView, ViewGroup parent) {
                //Use super class to create the View
                View v = super.getView(position, convertView, parent);
                TextView tv = v.findViewById(android.R.id.text1);
                tv.setText(newItem.get(position).app);
                tv.setTextSize(15.0f);
                //Put the image on the TextView
                tv.setCompoundDrawablesWithIntrinsicBounds(newItem.get(position).icon, null, null, null);

                //Add margin between image and text (support various screen densities)
                int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                tv.setCompoundDrawablePadding(dp5);

                return v;
            }
};

Note:- Make sure that DialogItem is your model class and you have to create in you app.

public class DialogItem {
    public String app = "";
    public Drawable icon;

    public DialogItem(String name, Drawable drawable) {
        app = name;
        icon = drawable;
    }
}

Step 3: Set that adapter in your AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Custom Sharing Dialog");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                ResolveInfo info = activities.get(item);

                if (info.activityInfo.packageName.equals("com.facebook.katana")) {
                    Toast.makeText(activity, "Facebook Selected ", Toast.LENGTH_LONG).show();
                } else {

                    // start the selected activity
                    Log.i(TAG, "Hi..hello. Intent is selected");
                    intent.setPackage(info.activityInfo.packageName);
                    startActivity(intent);
                }
            }
        });

AlertDialog alert = builder.create();
alert.show();

Output :-

enter image description here

Its custom sharable dialog using AlertDialog but you can do all setting(UI,Selection,Theme,etc) as per your requirement by using custom layout and creating Dialog class

Shailesh
  • 3,781
  • 3
  • 21
  • 44