Android - Clipboard and Keyboard

1. Copy text to the clipboard.
In Activity, use
val clipboard = getSystemService(ClipboardManager::class.java) val clipData = ClipData.newPlainText("The first copied text.", "Hello World!") clipboard.setPrimaryClip(clipData)

In Fragment, use
val clipboard = requireActivity().getSystemService(ClipboardManager::class.java) val clipData = ClipData.newPlainText("The first copied text.", "Hello World!") clipboard.setPrimaryClip(clipData)

2. Close the keyboard and the cursor.
In Activity, use
getSystemService(InputMethodManager::class.java) .hideSoftInputFromWindow(<The View object of scope>.windowToken, 0) currentFocus?.clearFocus()

In Fragment, use
requireActivity().getSystemService(InputMethodManager::class.java) .hideSoftInputFromWindow(<The View object of scope>.windowToken, 0) requireActivity().currentFocus?.clearFocus()

Comments

Popular posts from this blog

Manual - Modbus TCP Client

Manual - TCP / UDP Client

Manual - MQTT Client