How to use the Clipboard to copy and paste

Asked

Viewed 659 times

-1

How to use the clipboar android, to copy a text from a textView, I need to copy this text and use to paste elsewhere.

2 answers

1


Use a TextView with the property android:textIsSelectable=true

<TextView
    android:id="@+id/text_to_copy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true" />
  • Could I add this textview in a Alertdialog?

  • Yes, programmatically or via xml. If you want via xml, the way above you create a layout with this textview, and when creating the dialog you use the method setContentView of the Dialog class

  • @Hugofernandes managed to solve programmatically using Clipboard, but thanks for the help.

0

My solution:

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(DetalhesDebitosActivity.this);
                                        //alertDialogBuilder.setMessage(result);

                                        final TextView input = new TextView(DetalhesDebitosActivity.this);
                                        alertDialogBuilder.setView(input);

                                        input.setText(result);

                                        input.setTextIsSelectable(true);


                                        input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
                                        input.setTextSize(16);


        /*
                                        alertDialogBuilder.setNegativeButton("Ok",new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {


                                            }
                                        });*/


                                        alertDialogBuilder.setPositiveButton("Copiar",new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

                                                clipboard.setText(input.getText().toString());
                                                Toast.makeText(getApplicationContext(), "Linha digitável copiada!", Toast.LENGTH_SHORT).show();

                                                finish();


                                            }
                                        });



                                        AlertDialog alertDialog = alertDialogBuilder.create();
                                        alertDialog.show();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.