Android Chrome custom tabs add Copy Link link in options menu

How to add the option β€œCopy link” in the Chrome Custom Tabs options menu in Android. Adding custom menu items to CustomTabs is as follows.

CustomTabsIntent.Builder customTabsIntent = new CustomTabsIntent.Builder();

String menuItemTitle = App.s(R.string.share);
PendingIntent menuItemPendingIntent = createPendingIntentShare(url);
customTabsIntent.addMenuItem(menuItemTitle, menuItemPendingIntent);

I want to add the Copy Link option, as Twitter does in its application browser. I'm not sure how to copy a link to a Clipboard in CustomTabs.

enter image description here

+4
source share
1 answer

Create BroadcastReceiver:

public class CustomTabsBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String url = intent.getDataString();

        Toast.makeText(context, "Copy link pressed. URL = " + url, Toast.LENGTH_SHORT).show();

        //Here you can copy the URL to the clipboard
    }
}

Register it in AndroidManifest.xml:

<receiver
    android:name=".CustomTabsBroadcastReceiver"
    android:enabled="true">
</receiver>

Use this method to launch a custom tab:

private void launchCustomTab() {
    Intent intent = new Intent(this, CustomTabsBroadcastReceiver.class);

    String label = "Copy link";
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
            .addMenuItem(label, pendingIntent)
            .build();

    customTabsIntent.launchUrl(this, Uri.parse("http://www.google.it"));
}
+6
source

All Articles