How To Implement Copy/Paste in Android Using TextView
Implement copy/paste in your TextView in Android programmatically
In this article I’ll show you how to implement copy paste text from TextView in your Android app. EditText has built-in functionality whereby you can easily copy text through a long press but in the TextView, that might not be the case. We need to write some code to achieve the same functionality using our TextView.
Let’s get started!
You can check out the official documentation about Copy-Paste in Android here.
We’re making a simple app so I am starting with basic layout with one TextView.
onCreate() initialize your TextView and call
registerForContextMenu() method and pass your TextView in it.
According to the documentation
registerForContextMenu() helps to register a context menu to be shown for the given view, because we need a menu to select a “Copy” option that will pop up on a long press.
Now to override a method
onCreateContextMenu() , which will help us achieve our goal.
Now let’s run the app:
Found this article useful? Follow me Mustufa Ansari on medium. Check out my most read articles below.