Search code examples
androidlistviewbaseadapter

How to copy textview value from listview item to clipboard using context menu in android?


I want to copy textview value from listview item to clipboard using button click which is in the listview item. I can get textview value from listview item on listview onitemclick. But I want to get this value using context menu. So, how can i get the value from listview item. I have textview in Holder class.Here is my code:

    public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
            final ViewHolder holder;
            LayoutInflater li = getActivity().getLayoutInflater();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);

            if (v == null) {
                holder = new ViewHolder();
                v = inflater.inflate(R.layout.single_row, parent, false);
                holder.favourit_style = (ImageView) v.findViewById(R.id.favourit_style);
                holder.share_style = (ImageView) v.findViewById(R.id.share_style);
                holder.textView = (TextView) v.findViewById(R.id.text_Style);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }

            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    holder.textView.setText("" + s.toString() + "");

                }
            });
            registerForContextMenu(holder.share_style);
            return v;
        }

public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case COPY:
            ViewHolder h = new ViewHolder();
            String s = h.textView.getText().toString();
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText(s);
            } else {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", s);
                clipboard.setPrimaryClip(clip);
            }
            break;
   return super.onContextItemSelected(item);
}

Solution

  • First you have to get the String contents to be copied. I think that you can manage yourself. Now you can copy that content to clipboard using this code:

    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
    android.content.ClipData clip = android.content.ClipData.newPlainText("Clip", textToCopy);
    clipboard.setPrimaryClip(clip);
    

    Hope will help you!