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
?
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 own View
s.
Here are three methods of how you can archive your selectable text:
Since API-Level 11 (Android 3.0), the TextView
has a method called setTextIsSelectable()
, which pretty much looks like what I achieved by hacking around as detailed below.
// The TextView to show your Text
TextView showText = new TextView(this);
showText.setText("Some selectable text goes here.");
showText.setTextIsSelectable(true);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Build the Dialog
builder.setView(showText)
.setTitle("Selectable text")
.setCancelable(true)
.show();
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).
Since the purpose of marking text is (most of the time) to copy it, you can simply add an onLongClickListener
or a simple onClickListener
(the one you like the most) to the TextView
and copy its displayed text to the System's clipboard.
Little illustration of this:
// Get our tools
AlertDialog dialog;
AlertDialog.Builder builder;
// The TextView to show your Text
TextView showText = new TextView(this);
showText.setText("Some selectable text goes here.");
// Add the Listener
showText.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Copy the Text to the clipboard
ClipboardManager manager =
(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView showTextParam = (TextView) v;
manager.setText( showTextParam.getText() );
// Show a message:
Toast.makeText(v.getContext(), "Text in clipboard",
Toast.LENGTH_SHORT)
.show();
return true;
}
});
// Build the Dialog
builder = new AlertDialog.Builder(this);
builder.setView(showText);
dialog = builder.create();
// Some eye-candy
dialog.setTitle("Selectable text");
dialog.setCancelable(true);
dialog.show();
This example will copy all the displayed text in the systems clipboard.
EditText
look like a TextView
Since there is no really easy way to make the Text of a TextView
selectable on Android versions before 3.0, you can use an EditText
(which has selectable text by default) and make it look like a TextView
.
This needs some XML-Layout (to get the TextView
's style) but probably gets you the most native look and feel:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Make the EditText look like a TextView -->
<EditText android:id="@+id/showText"
style="?android:attr/textViewStyle"
android:background="@null"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
// Get our tools
AlertDialog dialog;
AlertDialog.Builder builder;
// The EditText to show your Text
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog,
(ViewGroup) this.findViewById(R.id.showText));
EditText showText = (EditText) layout.findViewById(R.id.showText);
showText.setText("Some selectable text goes here.");
// Build the Dialog
builder = new AlertDialog.Builder(this);
builder.setView(layout);
dialog = builder.create();
// Some eye-candy
dialog.setTitle("Selectable text");
dialog.setCancelable(true);
dialog.show();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With