Android share control

Nowadays, many applications provide the function of sharing information to the outside world. When sharing, the user usually pops up a list of all applications with sharing function from the bottom of the screen, and then selects them

Now I'm going to simulate this effect, not only to make the sharing control pop up from the bottom of the screen, but also to make the sharing control drag up and down, which requires the use of the BottomSheetDialog control provided by the design package

First, declare the main layout of the bottom sheet dialog dialog dialog [bottom] sheet.xml
Among them, RecyclerView is used to display the list of applications that provide sharing functions

<?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="match_parent"
    android:paddingBottom="14dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="14dp"
        android:text="Further explanation -> leavesC"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textSize="16sp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.6dp"
        android:background="#ddd"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:paddingStart="14dp"
        android:text="Share text information to..."
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textSize="14sp"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_appList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Layout item menu app.xml used by RecyclerView single child

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

    <ImageView
        android:id="@+id/iv_appIcon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/tv_appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:ellipsize="end"
        android:maxLength="6"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/iv_appIcon"
        tools:text="Literary jargon"/>

</android.support.constraint.ConstraintLayout>

Adapter used with RecyclerView: AppShareAdapter

/**
 * Author: leaves should be leaves
 * Time: 20:30, March 28, 2018
 * Description: https://github.com/leavesC
 */
public class AppShareAdapter extends RecyclerView.Adapter<AppShareAdapter.ViewHolder> {

    public interface OnClickListener {
        void onClick(int position);
    }

    public interface OnLongClickListener {
        void onLongClick(int position);
    }

    private List<App> appList;

    private LayoutInflater layoutInflater;

    private OnClickListener clickListener;

    private OnLongClickListener longClickListener;

    AppShareAdapter(Context context, List<App> appList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.appList = appList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_app, parent, false);
        return new AppShareAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.iv_appIcon.setBackground(appList.get(position).getAppIcon());
        holder.tv_appName.setText(appList.get(position).getAppName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onClick(holder.getAdapterPosition());
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (longClickListener != null) {
                    longClickListener.onLongClick(holder.getAdapterPosition());
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return appList.size();
    }

    void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    void setLongClickListener(OnLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_appIcon;

        private TextView tv_appName;

        ViewHolder(View itemView) {
            super(itemView);
            iv_appIcon = itemView.findViewById(R.id.iv_appIcon);
            tv_appName = itemView.findViewById(R.id.tv_appName);
        }

    }

}

Use Intent to find out all applications that provide sharing functions, and initialize the BottomSheetDialog

/**
 * Author: leaves should be leaves
 * Time: 20:30, March 28, 2018
 * Description: https://github.com/leavesC
 */
public class MainActivity extends AppCompatActivity {

    private List<App> appList;

    private BottomSheetDialog bottomSheetDialog;

    private Intent shareIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "https://github.com/leavesC");
    }

    public void originalShare(View view) {
        Intent intent = Intent.createChooser(shareIntent, "Share a text message");
        if (shareIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    public void customizedShare(View view) {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(this);
            bottomSheetDialog.setContentView(R.layout.dialog_bottom_sheet);
            initBottomDialog();
        }
        bottomSheetDialog.show();
    }

    private void initBottomDialog() {
        appList = getShareAppList(this, shareIntent);
        AppShareAdapter appShareAdapter = new AppShareAdapter(this, appList);
        appShareAdapter.setClickListener(new AppShareAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setComponent(new ComponentName(appList.get(position).getPackageName(), appList.get(position).getMainClassName()));
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, "https://github.com/leavesC");
                startActivity(intent);
            }
        });
        appShareAdapter.setLongClickListener(new AppShareAdapter.OnLongClickListener() {
            @Override
            public void onLongClick(int position) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + appList.get(position).getPackageName()));
                startActivity(intent);
            }
        });
        RecyclerView rv_appList = bottomSheetDialog.findViewById(R.id.rv_appList);
        if (rv_appList != null) {
            rv_appList.setLayoutManager(new GridLayoutManager(this, 4));
            rv_appList.setAdapter(appShareAdapter);
        }
    }

    public static List<App> getShareAppList(Context context, Intent intent) {
        List<App> appList = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
        if (resolveInfoList == null || resolveInfoList.size() == 0) {
            return null;
        } else {
            for (ResolveInfo resolveInfo : resolveInfoList) {
                App appInfo = new App(resolveInfo.loadLabel(packageManager).toString(), resolveInfo.activityInfo.packageName,
                        resolveInfo.activityInfo.name, resolveInfo.loadIcon(packageManager));
                appList.add(appInfo);
            }
        }
        return appList;
    }

}

The above example code is provided here: ShareDialog_Demo

Welcome to my GitHub account: leavesC

Tags: Android github xml encoding

Posted on Mon, 06 Apr 2020 00:35:39 -0700 by robvan75