The copy button in MCBFC is wired entirely insideDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tutosrive/mcbfc/llms.txt
Use this file to discover all available pages before exploring further.
BlockCodeEntry.Holder.init using the standard Android ClipboardManager API. No third-party libraries are required — everything needed is available in the Android framework from API level 11 onward. The click listener is registered once when the Holder is constructed and reused across every code block the holder is recycled into.
The copy flow
The click listener fires
btnCopy.setOnClickListener is invoked when the user taps the copy ImageButton. The lambda receives the clicked View as v, though it is not used directly in this implementation.Retrieve the rendered code text
commandText.text.trim() reads the current CharSequence from the TextView and strips any leading or trailing whitespace added by Markwon’s span rendering. Because the read happens at click time — not at bind time — the correct code is always captured even when the holder has been recycled and rebound to a different FencedCodeBlock.Obtain the system clipboard
Context.getSystemService returns the system-level ClipboardManager for the app’s process. The cast is safe because CLIPBOARD_SERVICE always returns a ClipboardManager on Android.Wrap the text in a ClipData
ClipData.newPlainText creates a single-item clip with the label "Command" and the code text as its plain-text payload. The label is shown by some system clipboard UIs (e.g. the clipboard history drawer on Samsung One UI) to help users identify the clip.Write to the clipboard
setPrimaryClip replaces whatever was previously on the clipboard with the new ClipData. On Android 13 (API 33) and above the system automatically shows a confirmation chip near the navigation bar, so you may choose to suppress the Toast on those versions.The button in the layout
TheImageButton is declared directly inside the root FrameLayout of fenced_code_view.xml, layered above the HorizontalScrollView using elevation:
fenced_code_view.xml
android:src="?attr/actionModeCopyDrawable"— references the system’s built-in copy action icon, so the button automatically adapts to the active theme (light, dark, or dynamic colour) without bundling any drawable resources.android:elevation="18dp"— places the button above theHorizontalScrollView(elevation8dp) and theTextView(elevation-1dp), ensuring it remains tappable and fully visible even when code text extends into the button’s area.android:layout_gravity="end"— pins the button to the top-right (end) corner of theFrameLayout, following the layout direction of the device locale.
The click listener code
The fullinit block from BlockCodeEntry.Holder that wires the click listener:
BlockCodeEntry.kt
Customization tips
- Branded icon — replace
?attr/actionModeCopyDrawablewith your own vector drawable resource (e.g.@drawable/ic_copy) to display a custom icon that matches your app’s design language. - Snackbar instead of Toast — swap the
Toastfor aSnackbaron the parentCoordinatorLayout. ASnackbarsupports an Undo action, which you can use to offer re-copy or navigation to the clipboard history. - Suppress the Toast on Android 13+ — from API 33 the system shows its own clipboard confirmation UI. Check
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISUand skip theToastto avoid a double confirmation. - Adjust z-ordering — if you add additional overlapping views (a language label badge, a line-count chip, etc.), tune
android:elevationon each view to control which surfaces appear on top. - Longer preview — the
text.take(7)preview can be extended to any length, or removed entirely if you prefer a plain “Copied!” message.