Reputation: 12467
I would like to allow the user to long press to pop up the "PASTE" mini-dialog over a TextView
. Once the user selects "PASTE", I would like to then intercept the value without updating the TextView
(note - I would manually update that TextView
later.
Is something like this possible without using an EditText
? I would prefer not to allow use of an EditText
because I'm using a custom in-app keyboard to allow input into an existing TextView
.
Upvotes: 0
Views: 97
Reputation: 1422
There is no native way in Android for that, but I wrote a sample code of how you can easily achieve what you want. You simply set a long click listener to textview and inside that you show AlertDialog for popup, which has an option to paste and when you click, it uses ClipboardManager to get the primary content in clipboard. Depending on your needs you can tweak the code and use anything in place of AlertDialog ( eg we used context menu before, but it cause glitches on various Android versions, so we replaced it with AlertDialog ). Let me know if you have questions.
textView.setLongClickable(true);
textView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(ArticleActivity.this, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(ArticleActivity.this);
}
builder.setTitle("Paste")
.setMessage("Are you sure you want to paste this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (clipboardManager != null) {
Log.e("TAG", "clipboard:" + clipboardManager.getPrimaryClip());
}
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
return true;
}
});
Upvotes: 2
Reputation: 770
Although this will work, there might be a better solution if you want your TextView
to act more like a standard EditText
and show a context menu.
You can do something like this
class SomeActivity : AppCompatActivity() {
override fun onCreate() {
// super.onCreate() and setContentView() calls ommited for clarity
// Assuming that pastableTextView is the TextView you want to paste into
registerForContextMenu(pastableTextView)
}
override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
// oversimplified for sake of example
if (v?.id == pastableTextView.id) {
menu?.add(0, v.id, 0, android.R.string.paste)
}
}
override fun onContextItemSelected(item: MenuItem?): Boolean {
// again, oversimplified for the sake of example
if (item?.itemId == pastableTextView.id) {
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager?
// This is clipboard's contents
val content = clipboardManager?.primaryClip?.getItemAt(0)?.text?
}
return false
}
}
Be aware, that this code isn't optimal, and I don't recommend using it for production app as it is.
Upvotes: 0
Reputation: 364
Another solution i would like to suggest is use edittext and hide the keyboard for that specific edittext just enable the copy and paste functions and its looks like Textview and if you dont want cursor blinks just disable that too.
Upvotes: 0