Maryada Jain
Maryada Jain

Reputation: 71

Copy Paste not working in android 2.3.6 (Gingerbread)

I hava a button and an edittext box in my activity ,on pressing the button i am copying all the text in the edittext to the Clipboard . The code is working fine in all the devices i checked except in Samsung GT-S6802 running with android version 2.3.6 . I am unable to discover the issue . Pls help .

btn3.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
                if (edit.getText().length() > 0) {
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                    clipboard.setText(edit.getText());
                    Toast.makeText(getApplicationContext(),
                            "Text is Copied Press long to paste anywhere",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

Upvotes: 2

Views: 1562

Answers (2)

Maryada Jain
Maryada Jain

Reputation: 71

For the version greater than HoneyComb ,the package of ClipboardManager is changed to android.content.ClipboardManager from android.text.ClipboardManager . Proper code is shown below .

 int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                            if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
                                android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                                ClipData clip = ClipData.newPlainText("label",
                                        edit.getText());
                                clipboard.setPrimaryClip(clip);
                            } else {
                                android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                                clipboard.setText(edit.getText());
                            }

Upvotes: 5

alphalima
alphalima

Reputation: 11

probably class android.content.ClipboardManager is imported, which works only from API>=11 on. Since 2.3.6 is API 10, the app crashes I guess saying java.lang.NoClassDefFoundError

See Android clipboard code that works on all API levels or How to copy text programmatically in my Android app? for solution that work. Still, the dalvikvm reports an error because it tries to verify all classes, but the app does not crash.

To prevent loading of classes, which can't be verified, see http://android-developers.blogspot.de/2010/07/how-to-have-your-cupcake-and-eat-it-too.html where the correct version dependent abstract class is instantiated at runtime.

Upvotes: 0

Related Questions