0

I have this code for copying to clipboard for API < 11

In my onCreate method :

TextView textView=(TextView)findViewById(R.id.textView1);
registerForContextMenu(textView);

Then override onCreateContextMenu :

@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");

    //cast the received View to TextView so that you can get its text
    TextView textView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    clipboard.setText(textView.getText()); }

and

 " android:textIsSelectable="true" " ,  for API >= 11 , 

How can I use if statement that if user's mobile is API < 11 the first code runs else the second runs ?????

Heena Goyal
  • 382
  • 3
  • 17
Ehsan
  • 5
  • 8

1 Answers1

1

If I understand correctly, you need something like this:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("deprecation")
public void copyToClipboard(String label, String text)
{
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        // Here we go if the device API level is less than 11, 
        // so we use old ClipboardManager class from "android.text" package
        android.text.ClipboardManager clipboard
                = (android.text.ClipboardManager) 
                getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        // Here we go if the device API highter than 11, 
        // so we use new ClipboardManager class from "android.content" package
        android.content.ClipboardManager clipboard
                = (android.content.ClipboardManager) 
                getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText(label, text);
        clipboard.setPrimaryClip(clip);
    }
}

Build.VERSION_CODES.HONEYCOMB corresponds to API level 11. This function copies the text to the clipboard using the old API on devices older API level 11 and new API on devices beyond API level 11.

Example function usage with your code:

@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");

    //cast the received View to TextView so that you can get its text
    TextView textView = (TextView) v;

    //place your TextView's text in clipboard
    copyToClipboard("Label describing text", textView.getText().toString());
}

And of course, before using copyToClipboard function you must copy it into yours Activity class.

Dmitry Ratty
  • 425
  • 1
  • 10
  • 13
  • I should add this under onCreate method ? – Ehsan Apr 01 '14 at 09:59
  • I am very biginner .... can you explain more in detail .... I have a textview .... I want the user with API both side of API 11 can copy to clipboard .... how can I use your code for this goal ?? – Ehsan Apr 01 '14 at 10:03
  • @Ehsan Your code will be executed regardless of the device API. Code which I posted copies the text to the clipboard using the old API on devices older API level 11 and new API on devices beyond API level 11. To use it in your example you should replace the last two lines of your onCreateContextMenu with call to my function. – Dmitry Ratty Apr 01 '14 at 10:06
  • @Ehsan you need to copy my code (copyToClipboard function that I provided) to your's Activity class, where also placed your onCreateContextMenu function. Than you need replace two last lines of your onCreateContextMenu function with call to my copyToClipboard function that you just copied. Or you can replace your copyToClipboard function with example from my post. – Dmitry Ratty Apr 01 '14 at 10:23
  • thank you but this error appears : The method copyToClipboard(String, String) in the type APIS is not applicable for the arguments (String, CharSequence) – Ehsan Apr 01 '14 at 10:26
  • @Ehsan than replace `copyToClipboard("Label describing text", textView.getText());` with `copyToClipboard("Label describing text", textView.getText().toString());`, I will update answer shortly. – Dmitry Ratty Apr 01 '14 at 10:34
  • It works but It isn't selectable for API >=11 .... my mobile is API > 11 but the textView isn't selectable ..... I wanted when API >= 11 you can select start and end in textview and then copy and when API < 11 you can copy all of text with pressing long on textview ...... this code can copy just all of text in both side of API 11 – Ehsan Apr 01 '14 at 10:45
  • @Ehsan look at this answer: http://stackoverflow.com/questions/6624763/android-copy-to-clipboard-selected-text-from-a-textview But there is solution only to copy end and start of text from TextView. As I understand its impossible to make normal TextView selectable on old API, instead you should use EditText as described here: http://stackoverflow.com/questions/10316298/select-copy-to-clipboard-in-textview-in-android – Dmitry Ratty Apr 01 '14 at 11:00
  • @Ehsan yeah, [isTextSelectable()](http://developer.android.com/reference/android/widget/TextView.html#isTextSelectable()) awaliable only from API level 11, so you can use EditText for both old and new API wit fixes from this answer: http://stackoverflow.com/questions/10316298/select-copy-to-clipboard-in-textview-in-android Or use EditText in old API by placing layout version for old API wit EditText in **layout** folder and layout version for new API with TextView in **layout-v11** folder. – Dmitry Ratty Apr 01 '14 at 11:08