Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devices with Android + MIUI and setCustomSelectionActionModeCallback

I'm trying to create custom selection menu but it does not work on a device with rom MIUI and Android 6. The result is common menu with copy and select all items. On other devices and simulators under clean Android it works just fine.

The code:

        textViewTop.setCustomSelectionActionModeCallback(new android.view.ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {

            Log.d(LOG_TAG, "onCreateActionMode");

            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            Log.d(LOG_TAG, "onPrepareActionMode");
            menu.clear();

            int quote_quick = R.drawable.ic_desktop_mac_black_24dp;
            int quote_add = R.drawable.ic_computer_black_24dp;
            int copy = R.drawable.ic_devices_other_black_24dp;

            menu.add(Menu.NONE, QUOTE_START, 3, "").setIcon(quote_quick).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            menu.add(Menu.NONE, QUOTE_ADD, 2, "").setIcon(quote_add).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            menu.add(Menu.NONE, CUSTOM_COPY, 1, "").setIcon(copy).setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
like image 668
micdev Avatar asked Jul 18 '17 11:07

micdev


1 Answers

So I figured out a workaround, but it makes sense only if you absolutely need it to work on MIUI devices. It's generally a little awkward:

I noticed that the Wikipedia app has custom actions working on a Xiaomi device, and after looking through the code I found out it works fine when the texts is selected in a WebView. You can basically use a WebView and override onActionModeStarted in your Activity

Acivity:

String html = "<!DOCTYPE html>\n" +
        "<html>\n" +
        "<head>\n" +
        "</head>\n" +
        "<body>\n" +
        "\n" +
        "<h1>WebView text</h1>\n" +
        "\n" +
        "</body>\n" +
        "</html>\n";

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

    WebView webView = findViewById(R.id.web_view);
    webView.setWebViewClient(new WebViewClient());
    webView.loadData(html, "text/html", "UTF-8");
}

@Override
public void onActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);
        Menu menu = mode.getMenu();
        menu.clear();
        mode.getMenuInflater().inflate(R.menu.menu_text_select, menu);
}

Menu:

<item android:id="@+id/id1"
      android:title="miui"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id2"
      android:title="has"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id3"
      android:title="bugs"
      app:showAsAction="ifRoom" />

<item android:id="@+id/id4"
    android:title="D:"
    app:showAsAction="ifRoom" />

Result: result

like image 108
Belzebub Avatar answered Nov 15 '22 01:11

Belzebub