ActionModeCallback does not work

2019-07-23 11:44发布

问题:

I am working on a task where I want to create custom tooltip on text selection. Means I want to add my own options in the tooltip menu. I used below code and it is working properly in some devices as shown in image.

In menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/ContextualActionModeTestOneActivity_add"
    android:icon="@android:drawable/ic_input_add"
    android:title="Add"
    android:titleCondensed="Add"
    app:showAsAction="ifRoom|withText" />

<item
    android:id="@+id/ContextualActionModeTestOneActivity_search"
    android:icon="@android:drawable/ic_menu_search"
    android:title="Search"
    android:titleCondensed="Search"
    app:showAsAction="ifRoom|withText" />

<item
    android:id="@+id/ContextualActionModeTestOneActivity_sort"
    android:icon="@android:drawable/ic_menu_sort_by_size"
    android:title="Sort"
    android:titleCondensed="Sort"
    app:showAsAction="ifRoom|withText" />
<item
    android:id="@+id/ContextualActionModeTestOneActivity_help"
    android:icon="@android:drawable/ic_menu_help"
    android:title="Help"
    android:titleCondensed="Help"
    app:showAsAction="ifRoom|withText" />

And below is my Activity code :

  private void initializeUI() {
  textView = (JustifiedTextView) findViewById(R.id.tv_big_data);
  textView.setText(getString(R.string.highlight_text));
  textView.setCustomSelectionActionModeCallback(new ActionBarCallBack());
  }


class ActionBarCallBack implements android.view.ActionMode.Callback {

    @Override
    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
        mode.setTitle("Do it");
        getMenuInflater().inflate(R.menu.contextual_action_mode_test_one_activity, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.ContextualActionModeTestOneActivity_add:
                Toast.makeText(getBaseContext(), "add this text somewhere ", Toast.LENGTH_LONG).show();
                break;
            case R.id.ContextualActionModeTestOneActivity_search:
                Toast.makeText(getBaseContext(), "search this text ", Toast.LENGTH_LONG).show();
                break;
            case R.id.ContextualActionModeTestOneActivity_sort:
                Toast.makeText(getBaseContext(), "sort", Toast.LENGTH_LONG).show();
                break;
            case R.id.ContextualActionModeTestOneActivity_help:
                Toast.makeText(getBaseContext(), "help with this", Toast.LENGTH_LONG).show();
                break;
        }
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {

    }


}

Problem is that it is not working in some devices For ex : Mi 4. It shows its default menu only and not our options like shown in below image.

Any suggestions..??