Android Share via Dialog

Android Share Via Dialog

This is indeed done with Intents.

For sharing an image, like in the example picture, it would be something like this:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/DCIM/Camera/myPic.jpg"));

startActivity(Intent.createChooser(share, "Share Image"));

For text you would use something like:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));

Open Share Dialog to Share a File USing ACTION_SEND

String fileName = ...
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"Share File"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "File Name");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(fileName)));
startActivity(Intent.createChooser(emailIntent, "Share File"));

Android - Share via Dialog has different layouts?

You have to create your custom chooser like this.

public class ShareAdapter extends BaseAdapter {

protected static final String TAG = "ShareAdapter";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
protected Context context;
protected List<ResolveInfo> list = new ArrayList<>();
protected PackageManager pm;

public ShareAdapter(Context context) {
Log.d(TAG, "ShareAdapter");
this.context = context;
pm = context.getPackageManager();
sendIntent.setType("image/*");
list = pm.queryIntentActivities(sendIntent, 0);
}

public void updateList(@NonNull List<ResolveInfo> list) {
this.list = list;
notifyDataSetChanged();
}

@Override
public int getCount() {
return list.size();
}

@Override
public ResolveInfo getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_chooser, parent, false);

holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.name = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();

holder.icon.setImageDrawable(getItem(position).loadIcon(pm));
holder.name.setText(getItem(position).loadLabel(pm));
return convertView;
}

static class ViewHolder {
ImageView icon;
TextView name;
}
}

public void showCustomChooser(final Uri uri) {
ShareAdapter shareAdapter = new ShareAdapter(context);
dialog = new Dialog(ShareActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(WMLP);
dialog.getWindow().setGravity(Gravity.BOTTOM);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimationBottom;
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.popup_chooser);
dialog.setCancelable(true);
ListView lv = (ListView) dialog.findViewById(R.id.listView);
ImageButton cancel = (ImageButton) dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

lv.setAdapter(shareAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
ResolveInfo launchable = shareAdapter.getItem(position);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.addCategory(Intent.CATEGORY_LAUNCHER);
sendIntent.setType("image/*");
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "some text");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setComponent(name);
context.startActivity(sendIntent);
dialog.dismiss();
}
});
dialog.show();
}

popup_chooser

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_margin="0dp"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@color/white"
android:orientation="horizontal">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:gravity="center"
android:text="@string/share_via"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/black_transparent_50" />

<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<ImageButton
android:id="@+id/cancel"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="@drawable/transparent_button_selector"
android:src="@android:drawable/btn_dialog"
android:visibility="gone" />
</LinearLayout>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black_transparent_12" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:fadeScrollbars="false"></ListView>

</LinearLayout>

</LinearLayout>

Item_chooser

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dp">

<ImageView
android:id="@+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="8dp"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:textColor="@color/black"
android:textSize="20dp" />

</LinearLayout>

Custom Image Sharing Dialog to other apps

Still Not Sure about the first issue, but found a solution to the second one .

Here is the code to create you own share dialog with whichever apps you want

final  List<Item> items=new ArrayList<>();

Intent audioIntent = new Intent(android.content.Intent.ACTION_SEND);
audioIntent.setDataAndType(Uri.parse("file://" + MainActivity.sSavedImagePath), "image/*");
List<ResolveInfo> audio = ExportOrSaveActivity.this.getPackageManager().queryIntentActivities(audioIntent, 0);
for (ResolveInfo info : audio){
String label = info.loadLabel(ExportOrSaveActivity.this.getPackageManager()).toString();
Drawable icon = info.loadIcon(ExportOrSaveActivity.this.getPackageManager());
String packageName = info.activityInfo.packageName;
String name = info.activityInfo.name;
Item ita=new Item(label , packageName ,icon );
if (!info.activityInfo.packageName.equalsIgnoreCase("com.package.toskip") && !info.activityInfo.packageName.equalsIgnoreCase("com.package.toskiptoo")) {
items.add(ita);
}

}

ListAdapter adapter = new ArrayAdapter<Item>(
this,
android.R.layout.select_dialog_item,
android.R.id.text1,
items){
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 = (TextView)v.findViewById(android.R.id.text1);

//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(items.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);
tv.setTextColor(R.color.black);

return v;
}
};

new AlertDialog.Builder(ExportOrSaveActivity.this,android.R.style.Theme_Holo_Light)
.setTitle("Share Image")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage(items.get(item).packagename);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + MainActivity.sSavedImagePath));
startActivity(share);

}
}).show();

Hope it help other who are looking for some thing similar.

Customise Sharing Intent Dialog Is That Possible?

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 :-

Sample Image

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

Sharing with android and kotlin

This should be helpful for you:

val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain"
val shareBody = "Text to be shared"
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody)
context.startActivity(Intent.createChooser(sharingIntent, "Share using ..."))


Related Topics



Leave a reply



Submit