Copy text from Android AlertDialog?
This is a bit hackish because it assumes you know the default AlertDialog layout (see Where can I find default AlertDialog layout xml file?), but if you want to go this way, you could do the following thing:
alertDialog.show();
try {
TextView textView = (TextView)dialog.getWindow().getDecorView().findViewById(android.R.id.message);
textView.setTextIsSelectable(true);
}
catch(Exception e) {
// Oups!
}
You could place an EditText in your AlertDialog. If you didn't want it to seem like there's a textbox in there, you could just style it like the background of the AlertDialog to make it blend in.
http://developer.android.com/guide/topics/ui/dialogs.html
Hopefully that helps, if not, I can try to provide other means of a solution.
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:
Using Android 3.0 +
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).
Copy to Clipboard
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.
Make an 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:
The XML-Part:
<?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>
The Java-Part:
// 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.