1

Following the answer here:

Intent i=new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject test");
i.putExtra(android.content.Intent.EXTRA_TEXT, "extra text that you want to put");
startActivity(Intent.createChooser(i,"Share via"));

On Lollipop it gives me something like this (a vertial list).

But sometimes I see a grid layout like this. For example: Youtube App's "share video."

Why is the layout different? How do I get the grid layout for the share dialog?

Community
  • 1
  • 1
Shawn
  • 2,675
  • 3
  • 25
  • 48
  • The second image looks like you are sharing some text since messaging is shown. The second you are sharing a link or file – OneCricketeer Jul 18 '16 at 03:16

2 Answers2

2

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>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Amit Shekhar
  • 3,135
  • 2
  • 16
  • 17
1

Its whatever the UI for that particular phone has installed. That differs based on android version, OEM, and possibly even launcher app. You can't control it, unless you want to write your own chooser activity.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127