Reputation: 190
I have used android clipboard manager to copy and paste text. Like whatsapp, i would like to copy multiple text from listview and paste those. I am able to do like this,
StringBuilder textMessage = new StringBuilder();
for(messsage) {
textmessage.append(message);
textmessage.append("\n");
}
ClipData clip = ClipData.newPlainText("simple text", textMessage.toString());
clipboard.setPrimaryClip(clip);
Instead of appending the multiple textmessages into one, can i able to store the array of textmessages into one clip object and retrive using array indices.
Upvotes: 2
Views: 2613
Reputation: 2075
I guess, you could add multiple ClipData.Item
to your ClipData
. So instead of using static method newPlainText
, create your new ClipData
using
ClipData(ClipDescription description, ClipData.Item item)
or any other constructor available.
I have used getItemCount
method of ClipData
to demonstrate that it is indeed a indexed list of values, so you can definitely use getItemAt
to fetch any ClipData.Item
from any position, provided position is not leading you to OutOfBoundException
. Below code is very novice, but would serve the purpose of demonstration I believe. Let me know if you need any more help.
public class MainActivity extends AppCompatActivity {
ClipboardManager clipboard;
static int var = 0;
ClipData clipData;
TextView tvClip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvClip = (TextView) findViewById(R.id.tv_add);
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
Button btnClip = (Button) findViewById(R.id.btn_add);
btnClip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipData.Item item = new ClipData.Item("var" + var);
if (clipData == null) {
clipData = new ClipData(new ClipDescription("your_clip_description", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}), item);
clipboard.setPrimaryClip(clipData);
}
clipData.addItem(item);
}
});
Button showClip = (Button) findViewById(R.id.btn_show);
showClip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clipData != null)
tvClip.setText("count = " + clipData.getItemCount());
}
});
}
}
Upvotes: 3