Is there any way to make the text in an AlertDialog “selectable” or “copyable” in Android? Or do I have to use some other widget instead of AlertDialog?
Is there any way to make the text in an AlertDialog selectable or copyable
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This can be achieved several ways. But first, to customize the dialog more, you’ll need to use the
AlertDialog.Builder-class to create your custom dialog. This can then be populated by your ownViews.Here are three methods of how you can archive your selectable text:
Using Android 3.0 +
Since API-Level 11 (Android 3.0), the
TextViewhas a method calledsetTextIsSelectable(), which pretty much looks like what I achieved by hacking around as detailed below.The problem with this solution is, that it will only work on devices running Android 3.0 and higher, while the other two solutions will also work on Android 1.5 (I used Android 2.2).
Copy to Clipboard
Since the purpose of marking text is (most of the time) to copy it, you can simply add an
onLongClickListeneror a simpleonClickListener(the one you like the most) to theTextViewand copy its displayed text to the System’s clipboard.Little illustration of this:
This example will copy all the displayed text in the systems clipboard.
Make an
EditTextlook like aTextViewSince there is no really easy way to make the Text of a
TextViewselectable on Android versions before 3.0, you can use anEditText(which has selectable text by default) and make it look like aTextView.This needs some XML-Layout (to get the
TextView‘s style) but probably gets you the most native look and feel:The XML-Part:
The Java-Part:
Some customizations and eye-candy might be added to the result 😉
I hope this gives you an idea of how you can achieve this task. It’s late now, time for bed.