0

Im working with my android project and it composed of multiple textview and recyclerview

I have used android:textIsSelectable="true" for my textview, but the problem is it select the one textview only.

Fragment Layout

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/text1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textIsSelectable="true"
       android:text="Title Here"/>

  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/text2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textIsSelectable="true"
       android:text=" AnotherTitle Here"/>

  <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/recyclerView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

Recycler view layout

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:text="Recycler view text here"/>

How I can copy all text at once by selecting them all when long press?

Paranoia
  • 37
  • 7

1 Answers1

0

Here are some links you can check out. Link for usage of Clipboard Official Documentation for clipboard usage

Implemenatation

  1. Make a <Button> that says "Copy All"
  2. Get your text from text fields using getText()
  3. Getting text from every item of recycler view won't be possible through xml so you have to get inside your code only. Because you will be sending data to adapter of your <RecyclerView> this means you already have your data. Simply use that.
  4. Finally using Clipboard Manager to copy all those text to clipboard
  5. Display a message to user saying "Text Copied"

Here is the code:

XML code:

        <Button
            android:id="@+id/copy_all_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Copy All" />

Java code:

//required imports
import android.content.ClipData;
import android.content.ClipboardManager;

public class MainActivity2 extends AppCompatActivity {
private List<String> itemsList;     // this list stores items of recycler view hence the text inside items of recycler view

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    itemsList = new ArrayList<>();  //this is your list
    itemsList.add("Item1 Text");    // I have added dummy items
    itemsList.add("Item2 Text");    //use as per your requirement from wherever you are getting list database of user input
    itemsList.add("Item3 Text");
    Button copyAllButton = findViewById(R.id.copy_all_button);  // button for copy all
    copyAllButton.setOnClickListener(view -> copyTextToClipboard());    //handling button click
}

//Call this method when button is clicked
private void copyTextToClipboard() {
    TextView tv1 = findViewById(R.id.text1);
    TextView tv2 = findViewById(R.id.text2);
    String st1 = tv1.getText().toString();   // get text from first text view
    String st2 = tv2.getText().toString();      // get text from second text view
    String recyclerText = getDataFromList();    // get data of items (but we already have it)
    String finalText = st1 + st2 + recyclerText;    // finally merge all text use comma or separator as per requirement

    //Now copy text to clipboard using ClipboardManager
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("label", finalText);
    clipboard.setPrimaryClip(clip);

    //Finally inform the user that text has been copied
    Toast.makeText(this, "Text Copied", Toast.LENGTH_SHORT).show();
}

//This method is used to get data from all items
private String getDataFromList() {
    StringBuilder sb = new StringBuilder();
    for (String s : itemsList) {
        sb.append(s).append(" ");
    }
    return sb.toString();
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186