Example usage for android.content ClipboardManager setText

List of usage examples for android.content ClipboardManager setText

Introduction

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

Prototype

@Deprecated
public void setText(CharSequence text) 

Source Link

Usage

From source file:com.scm.reader.resultPage.webview.ShortcutWebViewClient.java

private void overrideCopy(String text) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    }/*from  w  w  w.  j a  v a  2 s .c  om*/
}

From source file:com.espian.library.about.AbsAboutActivity.java

public void copy(String type, Uri uri, String backup) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        ClipboardManager newCm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        newCm.setPrimaryClip(ClipData.newUri(getContentResolver(), type, uri));

    } else {//from  w  w w  .j  av a 2 s.c om

        android.text.ClipboardManager oldCm = (android.text.ClipboardManager) getSystemService(
                Context.CLIPBOARD_SERVICE);
        oldCm.setText(backup);

    }
    Toast.makeText(this, getString(R.string.copy_to_clip, backup), Toast.LENGTH_SHORT).show();

}

From source file:org.gdgsp.fragment.EventFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_share:
        Intent sharePageIntent = new Intent();
        sharePageIntent.setAction(Intent.ACTION_SEND);
        sharePageIntent.putExtra(Intent.EXTRA_TEXT, event.getName() + " " + event.getLink());
        sharePageIntent.setType("text/plain");
        startActivity(Intent.createChooser(sharePageIntent, getResources().getText(R.string.share)));
        return true;
    case R.id.menu_people:
        Intent intent = new Intent(activity, PeopleActivity.class);
        intent.putExtra("event", event);
        startActivity(intent);//w ww  . ja va2 s.  c  om
        return true;
    case R.id.menu_copylink:
        if (Build.VERSION.SDK_INT <= 10) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) activity
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(event.getLink());
        } else {
            ClipboardManager clipboardManager = (ClipboardManager) activity
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText(event.getLink(), event.getLink());
            clipboardManager.setPrimaryClip(clipData);
        }
        Other.showToast(activity, getString(R.string.link_copyed));
        return true;
    case R.id.menu_openinbrowser:
        Other.openSite(activity, event.getLink());
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:de.echtzeitraum.openpassword.MainView.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Updates App title
    this.setTitle(this.getResources().getString(R.string.app_title));

    // Enables progress icon if API level >= 11 */
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setSupportProgressBarIndeterminateVisibility(false);
    }//from  w  ww .  j  a va 2  s . c o m

    MainView.passwordGenerator = new PasswordGenerator();
    MainView.settings = getSharedPreferences(PREFS_NAME, 0);

    setContentView(R.layout.main);

    mAdapter = new ModeAdapter(getSupportFragmentManager());

    // create fragments to use
    if (savedInstanceState != null) {
        this.fSimple = (SimpleFragment) getSupportFragmentManager().getFragment(savedInstanceState,
                SimpleFragment.class.getName());
        this.fAdvanced = (AdvancedFragment) getSupportFragmentManager().getFragment(savedInstanceState,
                AdvancedFragment.class.getName());
    }
    if (this.fSimple == null)
        this.fSimple = new SimpleFragment();
    if (this.fAdvanced == null)
        this.fAdvanced = new AdvancedFragment();

    mAdapter.add(this.fSimple);
    mAdapter.add(this.fAdvanced);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.removeAllViews();
    mPager.setAdapter(mAdapter);

    int position = MainView.settings.getInt("fragment", 0);
    mPager.setCurrentItem(position);

    final Button button = (Button) this.findViewById(R.id.ok);

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            generatePassword();
        }
    });

    final EditText pwField = (EditText) findViewById(R.id.password);
    pwField.setLongClickable(true);
    pwField.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            /* Copy text to clipboard manager */
            /* Use new ClipboardManager if API level >= 11 */
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(
                        CLIPBOARD_SERVICE);
                clipboard.setText(pwField.getText());
            } else {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
                        CLIPBOARD_SERVICE);
                clipboard.setText(pwField.getText());
            }

            /* Show message: Password copied to clipboard */
            Toast.makeText(getApplicationContext(), R.string.copy, Toast.LENGTH_SHORT).show();

            return true;
        }
    });
}

From source file:net.vivekiyer.GAL.Configure.java

@SuppressWarnings("deprecation")
@TargetApi(11)//  w ww . j  av  a2s . c  om
@Override
public void onChoiceDialogOptionPressed(int action) {
    switch (action) {
    case android.R.id.copy:
        if (Utility.isPreHoneycomb()) {
            final android.text.ClipboardManager clipboard;
            clipboard = (android.text.ClipboardManager) getSystemService(
                    android.content.Context.CLIPBOARD_SERVICE);
            clipboard.setText(activeSyncManager.getDeviceId());
        } else {
            ClipboardManager clip = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            clip.setPrimaryClip(ClipData.newPlainText("Android Device ID", activeSyncManager.getDeviceId())); //$NON-NLS-1$
        }
        break;
    }
}

From source file:com.example.linhdq.test.documents.viewing.single.DocumentActivity.java

@SuppressWarnings("deprecation")
private void copyTextToClipboard(String text) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
            Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);
}

From source file:com.umeng.message.example.MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void copyToClipBoard() {
    if (Build.VERSION.SDK_INT < 11)
        return;/*w  ww.  j  av a  2s.c  o  m*/
    String deviceToken = mPushAgent.getRegistrationId();
    if (!TextUtils.isEmpty(deviceToken)) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        clipboard.setText(deviceToken);
        toast("DeviceToken???");
    }
}

From source file:com.eng.arab.translator.androidtranslator.translate.TranslateViewActivity.java

private void initTranslatorPanel() {
    mainPanel = (LinearLayout) findViewById(R.id.activity_translator_translate);

    srcCard = (CardView) findViewById(R.id.src_card);
    srcToolbar = (Toolbar) findViewById(R.id.src_toolbar);
    srcToolbar.inflateMenu(R.menu.src_card);
    srcToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override//from  ww  w  .ja  v  a  2s. c o m
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.action_paste:
                srcText.setText(readFromClipboard());
                trgText.setTextColor(Color.BLACK);// Set Default Color
                showAToast("Pasted");
                break;
            /*case R.id.action_share:
                if (keyboard_flag == 0) {
                    hideSoftKeyboard();
                    keyboard_flag = 1;
                }
                else keyboard_flag = 0;
                Toast.makeText(TranslateViewActivity.this,"Share",Toast.LENGTH_SHORT).show();
                break;*/
            case R.id.action_clear:
                showAToast("Cleared");
                srcText.setText("");
                trgText.setTextColor(Color.BLACK);// Set Default Color
                //trgText.setText("");
                break;
            }
            return true;
        }
    });
    srcToolbar.setOnClickListener(this);
    srcToolbar.setTitle("ARABIC");/**UPPER Text to translate*/
    srcContent = (LinearLayout) findViewById(R.id.src_translate_content);

    translateButton = (Button) findViewById(R.id.main_translate_button);
    translateButton.setOnClickListener(this);

    trgCard = (CardView) findViewById(R.id.translate_trg_card);
    trgToolbar = (Toolbar) findViewById(R.id.translate_trg_toolbar);
    trgToolbar.inflateMenu(R.menu.trg_card);
    trgToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {

            case R.id.action_trg_audio:
                //Toast.makeText(TranslateViewActivity.this,"TRG AUDIO",Toast.LENGTH_SHORT).show();
                speakEnglishTranslation();
                break;
            case R.id.action_copy:
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(
                        Context.CLIPBOARD_SERVICE);
                clipboard.setText(trgText.getText());
                showAToast("English translation copied");
                trgText.setTextColor(Color.BLUE); // Set color as Higlight
                break;
            /*case R.id.action_share:
                Toast.makeText(TranslateViewActivity.this,"Share",Toast.LENGTH_SHORT).show();
                final Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TEXT, trgText.getText().toString());
                startActivity(Intent.createChooser(intent, getResources().getText(R.string.share_with)));
                break;*/
            }
            return true;
        }
    });
    trgToolbar.setOnClickListener(this);
    trgToolbar.setTitle("ENGLISH");/**LOWER Translated*/
    trgContent = (LinearLayout) findViewById(R.id.trg_content);

    srcText = (EditText) findViewById(R.id.src_text);

    trgTextScroll = (ScrollView) findViewById(R.id.trg_text_scroll);
    trgText = (TextView) findViewById(R.id.trg_text);

    mainPanel.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        private int trgCardPreEditingVisibility;

        @Override
        public void onGlobalLayout() {
            final int heightDiffPx = mainPanel.getRootView().getHeight() - mainPanel.getHeight();
            final int heightDiffDp = (int) (heightDiffPx
                    / (getResources().getDisplayMetrics().densityDpi / 160f));
            if (heightDiffDp > 150 && !editing) { // if more than 150 dps, its probably a keyboard...
                editing = true;
                trgCardPreEditingVisibility = trgCard.getVisibility();
                trgCard.setVisibility(View.GONE);
                //                    updateSrcToolbar();
            } else if (heightDiffDp < 150 && editing) {
                editing = false;
                trgCard.setVisibility(trgCardPreEditingVisibility);
                srcText.clearFocus();
                //                    updateSrcToolbar();
            }
        }

    });

    final Intent intent = getIntent();
    if (Intent.ACTION_SEND.equals(intent.getAction()) && "text/plain".equals(intent.getType())) {
        srcText.setText(intent.getStringExtra(Intent.EXTRA_TEXT));
    }
    //snackBar();
}

From source file:com.shaweibo.biu.ui.timeline.StatusDetailActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    switch (id) {
    case R.id.action_favo:
        FavoDao dao = new FavoDao(mWeibo.id, StatusDetailActivity.this);
        dao.favo();//from w w  w . j a  va  2  s .  c om
        break;
    case R.id.action_copy:
        ClipboardManager c = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        c.setText(tv_content.getText());
        break;
    case R.id.action_comment:
        PostNewCommentActivity.start(this, mWeibo);
        break;
    case R.id.action_repost:
        PostNewRepostActivity.start(this, mWeibo);
        break;
    case android.R.id.home:
        onBackPressed();
        break;

    }

    return super.onOptionsItemSelected(item);
}

From source file:org.kde.kdeconnect.Plugins.SharePlugin.SharePlugin.java

@Override
public boolean onPackageReceived(NetworkPackage np) {

    try {//from w  ww .ja  v a 2  s.c om
        if (np.hasPayload()) {

            Log.i("SharePlugin", "hasPayload");

            final InputStream input = np.getPayload();
            final long fileLength = np.getPayloadSize();
            final String originalFilename = np.getString("filename", Long.toString(System.currentTimeMillis()));

            //We need to check for already existing files only when storing in the default path.
            //User-defined paths use the new Storage Access Framework that already handles this.
            final boolean customDestination = ShareSettingsActivity.isCustomDestinationEnabled(context);
            final String defaultPath = ShareSettingsActivity.getDefaultDestinationDirectory().getAbsolutePath();
            final String filename = customDestination ? originalFilename
                    : FilesHelper.findNonExistingNameForNewFile(defaultPath, originalFilename);

            String displayName = FilesHelper.getFileNameWithoutExt(filename);
            final String mimeType = FilesHelper.getMimeTypeFromFile(filename);

            if ("*/*".equals(mimeType)) {
                displayName = filename;
            }

            final DocumentFile destinationFolderDocument = ShareSettingsActivity
                    .getDestinationDirectory(context);
            final DocumentFile destinationDocument = destinationFolderDocument.createFile(mimeType,
                    displayName);
            final OutputStream destinationOutput = context.getContentResolver()
                    .openOutputStream(destinationDocument.getUri());
            final Uri destinationUri = destinationDocument.getUri();

            final int notificationId = (int) System.currentTimeMillis();
            Resources res = context.getResources();
            final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                    .setContentTitle(res.getString(R.string.incoming_file_title, device.getName()))
                    .setContentText(res.getString(R.string.incoming_file_text, filename))
                    .setTicker(res.getString(R.string.incoming_file_title, device.getName()))
                    .setSmallIcon(android.R.drawable.stat_sys_download).setAutoCancel(true).setOngoing(true)
                    .setProgress(100, 0, true);

            final NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());

            new Thread(new Runnable() {
                @Override
                public void run() {
                    boolean successful = true;
                    try {
                        byte data[] = new byte[1024];
                        long progress = 0, prevProgressPercentage = 0;
                        int count;
                        while ((count = input.read(data)) >= 0) {
                            progress += count;
                            destinationOutput.write(data, 0, count);
                            if (fileLength > 0) {
                                if (progress >= fileLength)
                                    break;
                                long progressPercentage = (progress * 100 / fileLength);
                                if (progressPercentage != prevProgressPercentage) {
                                    prevProgressPercentage = progressPercentage;
                                    builder.setProgress(100, (int) progressPercentage, false);
                                    NotificationHelper.notifyCompat(notificationManager, notificationId,
                                            builder.build());
                                }
                            }
                            //else Log.e("SharePlugin", "Infinite loop? :D");
                        }

                        destinationOutput.flush();

                    } catch (Exception e) {
                        successful = false;
                        Log.e("SharePlugin", "Receiver thread exception");
                        e.printStackTrace();
                    } finally {
                        try {
                            destinationOutput.close();
                        } catch (Exception e) {
                        }
                        try {
                            input.close();
                        } catch (Exception e) {
                        }
                    }

                    try {
                        Log.i("SharePlugin", "Transfer finished: " + destinationUri.getPath());

                        //Update the notification and allow to open the file from it
                        Resources res = context.getResources();
                        String message = successful
                                ? res.getString(R.string.received_file_title, device.getName())
                                : res.getString(R.string.received_file_fail_title, device.getName());
                        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                                .setContentTitle(message).setTicker(message)
                                .setSmallIcon(android.R.drawable.stat_sys_download_done).setAutoCancel(true);
                        if (successful) {
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setDataAndType(destinationUri, mimeType);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                            stackBuilder.addNextIntent(intent);
                            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                                    PendingIntent.FLAG_UPDATE_CURRENT);
                            builder.setContentText(
                                    res.getString(R.string.received_file_text, destinationDocument.getName()))
                                    .setContentIntent(resultPendingIntent);
                        }
                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                        if (prefs.getBoolean("share_notification_preference", true)) {
                            builder.setDefaults(Notification.DEFAULT_ALL);
                        }
                        NotificationHelper.notifyCompat(notificationManager, notificationId, builder.build());

                        if (successful) {
                            if (!customDestination && Build.VERSION.SDK_INT >= 12) {
                                Log.i("SharePlugin", "Adding to downloads");
                                DownloadManager manager = (DownloadManager) context
                                        .getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.addCompletedDownload(destinationUri.getLastPathSegment(),
                                        device.getName(), true, mimeType, destinationUri.getPath(), fileLength,
                                        false);
                            } else {
                                //Make sure it is added to the Android Gallery anyway
                                MediaStoreHelper.indexFile(context, destinationUri);
                            }
                        }

                    } catch (Exception e) {
                        Log.e("SharePlugin", "Receiver thread exception");
                        e.printStackTrace();
                    }
                }
            }).start();

        } else if (np.has("text")) {
            Log.i("SharePlugin", "hasText");

            String text = np.getString("text");
            if (Build.VERSION.SDK_INT >= 11) {
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(text);
            } else {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                clipboard.setText(text);
            }
            Toast.makeText(context, R.string.shareplugin_text_saved, Toast.LENGTH_LONG).show();
        } else if (np.has("url")) {

            String url = np.getString("url");

            Log.i("SharePlugin", "hasUrl: " + url);

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (openUrlsDirectly) {
                context.startActivity(browserIntent);
            } else {
                Resources res = context.getResources();
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addNextIntent(browserIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Notification noti = new NotificationCompat.Builder(context)
                        .setContentTitle(res.getString(R.string.received_url_title, device.getName()))
                        .setContentText(res.getString(R.string.received_url_text, url))
                        .setContentIntent(resultPendingIntent)
                        .setTicker(res.getString(R.string.received_url_title, device.getName()))
                        .setSmallIcon(R.drawable.ic_notification).setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL).build();

                NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationHelper.notifyCompat(notificationManager, (int) System.currentTimeMillis(), noti);
            }
        } else {
            Log.e("SharePlugin", "Error: Nothing attached!");
        }

    } catch (Exception e) {
        Log.e("SharePlugin", "Exception");
        e.printStackTrace();
    }

    return true;
}