Example usage for android.text ClipboardManager setText

List of usage examples for android.text ClipboardManager setText

Introduction

In this page you can find the example usage for android.text ClipboardManager setText.

Prototype

public abstract void setText(CharSequence text);

Source Link

Document

Sets the contents of the clipboard to the specified text.

Usage

From source file:com.application.akscorp.yandextranslator2017.TranslateScreen.java

private void CopyTextToClipBoard(String text) {
    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(text);
    } else {//from   w ww.j a v a 2  s .  c o m
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity()
                .getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", text);
        clipboard.setPrimaryClip(clip);
    }
    Toast.makeText(context, LanguageWork.GetResourceString(context, "copy_text"), Toast.LENGTH_SHORT).show();
}

From source file:com.sentaroh.android.TextFileBrowser.FileViewerFragment.java

private String copyToClipboard(int from_line, int to_line, FileViewerAdapter adapter) {
    ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    String sep = "";
    int c_line = 0;
    StringBuilder out = new StringBuilder("");
    for (int i = from_line; i <= to_line; i++) {
        c_line++;//from  www  . j  av  a 2  s  . com
        out.append(sep);
        out.append(adapter.getItem(i)[0]);
        sep = "\n";
    }
    cm.setText(out);
    return String.format(getString(R.string.msgs_text_browser_copymsg_copied), c_line);
}

From source file:com.xgf.inspection.qrcode.google.zxing.client.CaptureActivity.java

private void handleDecodeExternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {

    if (barcode != null) {
        viewfinderView.drawResultBitmap(barcode);
    }//  w  w w.ja v  a 2 s  .  c  om

    long resultDurationMS;
    if (getIntent() == null) {
        resultDurationMS = DEFAULT_INTENT_RESULT_DURATION_MS;
    } else {
        resultDurationMS = getIntent().getLongExtra(Intents.Scan.RESULT_DISPLAY_DURATION_MS,
                DEFAULT_INTENT_RESULT_DURATION_MS);
    }

    // Since this message will only be shown for a second, just tell the
    // user what kind of
    // barcode was found (e.g. contact info) rather than the full contents,
    // which they won't
    // have time to read.
    if (resultDurationMS > 0) {
        statusView.setText(getString(resultHandler.getDisplayTitle()));
    }

    if (copyToClipboard && !resultHandler.areContentsSecure()) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        CharSequence text = resultHandler.getDisplayContents();
        if (text != null) {
            clipboard.setText(text);
        }
    }

    if (source == IntentSource.NATIVE_APP_INTENT) {

        // Hand back whatever action they requested - this can be changed to
        // Intents.Scan.ACTION when
        // the deprecated intent is retired.
        Intent intent = new Intent(getIntent().getAction());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
        intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
        byte[] rawBytes = rawResult.getRawBytes();
        if (rawBytes != null && rawBytes.length > 0) {
            intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
        }
        Map<ResultMetadataType, ?> metadata = rawResult.getResultMetadata();
        if (metadata != null) {
            if (metadata.containsKey(ResultMetadataType.UPC_EAN_EXTENSION)) {
                intent.putExtra(Intents.Scan.RESULT_UPC_EAN_EXTENSION,
                        metadata.get(ResultMetadataType.UPC_EAN_EXTENSION).toString());
            }
            Integer orientation = (Integer) metadata.get(ResultMetadataType.ORIENTATION);
            if (orientation != null) {
                intent.putExtra(Intents.Scan.RESULT_ORIENTATION, orientation.intValue());
            }
            String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
            if (ecLevel != null) {
                intent.putExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL, ecLevel);
            }
            Iterable<byte[]> byteSegments = (Iterable<byte[]>) metadata.get(ResultMetadataType.BYTE_SEGMENTS);
            if (byteSegments != null) {
                int i = 0;
                for (byte[] byteSegment : byteSegments) {
                    intent.putExtra(Intents.Scan.RESULT_BYTE_SEGMENTS_PREFIX + i, byteSegment);
                    i++;
                }
            }
        }
        sendReplyMessage(R.id.return_scan_result, intent, resultDurationMS);

    } else if (source == IntentSource.PRODUCT_SEARCH_LINK) {

        // Reformulate the URL which triggered us into a query, so that the
        // request goes to the same
        // TLD as the scan URL.
        int end = sourceUrl.lastIndexOf("/scan");
        String replyURL = sourceUrl.substring(0, end) + "?q=" + resultHandler.getDisplayContents()
                + "&source=zxing";
        sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);

    } else if (source == IntentSource.ZXING_LINK) {

        // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the
        // returnUrlTemplate
        // with the scanned code. This allows both queries and REST-style
        // URLs to work.
        if (returnUrlTemplate != null) {
            CharSequence codeReplacement = returnRaw ? rawResult.getText() : resultHandler.getDisplayContents();
            try {
                codeReplacement = URLEncoder.encode(codeReplacement.toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // can't happen; UTF-8 is always supported. Continue, I
                // guess, without encoding
            }
            String replyURL = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER, codeReplacement);
            sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
        }

    }
}

From source file:org.totschnig.myexpenses.activity.MyExpenses.java

public void copyToClipBoard(View view) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(mCurrentBalance);
    Toast.makeText(this, R.string.copied_to_clipboard, Toast.LENGTH_LONG).show();
}

From source file:com.googlecode.networklog.LogFragment.java

@SuppressWarnings("deprecation")
public void copyDestIp(ListItem item) {
    String dstAddr;/*www .  j a  va2 s. co m*/
    String dstPort;

    if (NetworkLog.resolveHosts && NetworkLog.resolveCopies) {
        String resolved = NetworkLog.resolver.resolveAddress(item.dstAddr);

        if (resolved != null) {
            dstAddr = resolved;
        } else {
            dstAddr = item.dstAddr;
        }
    } else {
        dstAddr = item.dstAddr;
    }

    if (NetworkLog.resolvePorts && NetworkLog.resolveCopies) {
        dstPort = NetworkLog.resolver.resolveService(String.valueOf(item.dstPort));
    } else {
        dstPort = String.valueOf(item.dstPort);
    }

    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

    /* newer API 11 clipboard unsupported on older devices
       ClipData clip = ClipData.newPlainText("NetworkLog Dest IP", dstAddr + ":" + dstPort);
       clipboard.setPrimaryClip(clip);
       */

    /* use older deprecated ClipboardManager to support older devices */
    clipboard.setText(dstAddr + ":" + dstPort);
}

From source file:com.googlecode.networklog.LogFragment.java

@SuppressWarnings("deprecation")
public void copySourceIp(ListItem item) {
    String srcAddr;//from  w  ww.j a va  2s .co  m
    String srcPort;

    if (NetworkLog.resolveHosts && NetworkLog.resolveCopies) {
        String resolved = NetworkLog.resolver.resolveAddress(item.srcAddr);

        if (resolved != null) {
            srcAddr = resolved;
        } else {
            srcAddr = item.srcAddr;
        }
    } else {
        srcAddr = item.srcAddr;
    }

    if (NetworkLog.resolvePorts && NetworkLog.resolveCopies) {
        srcPort = NetworkLog.resolver.resolveService(String.valueOf(item.srcPort));
    } else {
        srcPort = String.valueOf(item.srcPort);
    }

    ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

    /* newer API 11 clipboard unsupported on older devices
       ClipData clip = ClipData.newPlainText("NetworkLog Source IP", srcAddr + ":" + srcPort);
       clipboard.setPrimaryClip(clip);
       */

    /* use older deprecated ClipboardManager to support older devices */
    clipboard.setText(srcAddr + ":" + srcPort);
}

From source file:org.cryptsecure.Utility.java

/**
 * Copy text to clipboard. This method checks for honeycomp changes to the
 * clipboard.//from  w ww  .  j  a va2 s  .co m
 * 
 * @param context
 *            the context
 * @param text
 *            the text
 */
@SuppressWarnings("deprecation")
public static void copyToClipboard(Context context, String text) {
    if (text != null) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData.newPlainText("WordKeeper", text);
            clipboard.setPrimaryClip(clip);
        }
    }
}

From source file:com.codebutler.farebot.fragments.CardsFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {//from   w  w w  . j  a v  a  2 s.  c  o m
        if (item.getItemId() == R.id.import_clipboard) {
            @SuppressWarnings("deprecation")
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Activity.CLIPBOARD_SERVICE);
            onCardsImported(ExportHelper.importCardsXml(getActivity(), clipboard.getText().toString()));
            return true;

        } else if (item.getItemId() == R.id.import_file) {
            Uri uri = Uri.fromFile(Environment.getExternalStorageDirectory());
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.putExtra(Intent.EXTRA_STREAM, uri);
            i.setType("application/xml");
            startActivityForResult(Intent.createChooser(i, "Select File"), REQUEST_SELECT_FILE);
            return true;

        } else if (item.getItemId() == R.id.import_sd) {
            String xml = FileUtils.readFileToString(new File(SD_EXPORT_PATH));
            onCardsImported(ExportHelper.importCardsXml(getActivity(), xml));
            return true;

        } else if (item.getItemId() == R.id.copy_xml) {
            @SuppressWarnings("deprecation")
            ClipboardManager clipboard = (ClipboardManager) getActivity()
                    .getSystemService(Activity.CLIPBOARD_SERVICE);
            clipboard.setText(ExportHelper.exportCardsXml(getActivity()));
            Toast.makeText(getActivity(), "Copied to clipboard.", 5).show();
            return true;

        } else if (item.getItemId() == R.id.share_xml) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, ExportHelper.exportCardsXml(getActivity()));
            startActivity(intent);
            return true;

        } else if (item.getItemId() == R.id.save_xml) {
            String xml = ExportHelper.exportCardsXml(getActivity());
            File file = new File(SD_EXPORT_PATH);
            FileUtils.writeStringToFile(file, xml, "UTF-8");
            Toast.makeText(getActivity(), "Wrote FareBot-Export.xml to USB Storage.", 5).show();
            return true;
        }
    } catch (Exception ex) {
        Utils.showError(getActivity(), ex);
    }
    return false;
}

From source file:com.edible.ocr.CaptureActivity.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    switch (item.getItemId()) {

    case OPTIONS_COPY_RECOGNIZED_TEXT_ID:
        clipboardManager.setText(ocrResultView.getText());
        if (clipboardManager.hasText()) {
            Toast toast = Toast.makeText(this, "Text copied.", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();/*from  ww  w  .j a  va  2s  .  c o m*/
        }
        return true;
    case OPTIONS_SHARE_RECOGNIZED_TEXT_ID:
        Intent shareRecognizedTextIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareRecognizedTextIntent.setType("text/plain");
        shareRecognizedTextIntent.putExtra(android.content.Intent.EXTRA_TEXT, ocrResultView.getText());
        startActivity(Intent.createChooser(shareRecognizedTextIntent, "Share via"));
        return true;
    case OPTIONS_COPY_TRANSLATED_TEXT_ID:
        clipboardManager.setText(translationView.getText());
        if (clipboardManager.hasText()) {
            Toast toast = Toast.makeText(this, "Text copied.", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();
        }
        return true;
    case OPTIONS_SHARE_TRANSLATED_TEXT_ID:
        Intent shareTranslatedTextIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareTranslatedTextIntent.setType("text/plain");
        shareTranslatedTextIntent.putExtra(android.content.Intent.EXTRA_TEXT, translationView.getText());
        startActivity(Intent.createChooser(shareTranslatedTextIntent, "Share via"));
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

From source file:com.hughes.android.dictionary.DictionaryActivity.java

@SuppressWarnings("deprecation")
void onCopy(final RowBase row) {
    defocusSearchText();/*  ww w. j  ava2s.  c o  m*/

    Log.d(LOG, "Copy, row=" + row);
    final StringBuilder result = new StringBuilder();
    result.append(row.getRawText(false));
    final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setText(result.toString());
    Log.d(LOG, "Copied: " + result);
}