Message Actions

The DeliveredConversationFragment supports message selection and actions such as copy and delete. This allows you to select one or multiple messages and choose the action you want to perform on them. If you have not set up your conversation Activity yet, please refer to the Initial setup section before continuing.

This feature is made possible by the ActionMode class, part of the Android SDK. Make sure you follow the the instructions here Using the contextual action mode to enable this feature.

Once you have enabled ActionMode, implement the DeliveredConversationFragment’s interface OnMessageSelectListener in your conversation Activity and override the necessary methods:

public class ConversationActivity extends AppCompatActivity
        implements OnMessageSelectListener, ActionMode.Callback {

    private DeliveredConversationFragment mConversationFragment;
    private String mChatId;

    private ActionMode actionMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);
        
        ...

        mConversationFragment.setOnMessageSelectListener(this);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.menu_action_conversation, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        menu.findItem(R.id.copy_message).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.findItem(R.id.delete_message).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.copy_message:
                mConversationFragment.copySelectedMessages();
                actionMode.finish();
                break;
            case R.id.delete_message:
                mConversationFragment.deleteSelectedMessages();
                actionMode.finish();
                break;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        if (actionMode != null) {
            mConversationFragment.clearMessageListSelections();
            actionMode = null;
        }
    }

    @Override
    public void onMessageSelect(int selectionCount) {
        if (actionMode == null) {
            actionMode = startSupportActionMode(this);
        }

        if (actionMode != null) {
            actionMode.setTitle(String.valueOf(selectionCount));
        }

        if (actionMode != null && selectionCount == 0) {
            actionMode.finish();
        }
    }
}

The previous code snippet shows how the DeliveredConversationFragment communicates with the ActionMode feature. As a result you should be able to select, copy and delete one or more messages:

message_select.png