3

I want to test the clipboard on the Android API, so I set out to create a simple application using the Android's ClipboardManager and ClipData classes. I followed the Copy and Paste guide on the Android website, although I had to fill in several spots as this guide doesn't have every line of code necessary (I eventually had to comment out the Intent and URI paste methods because it doesn't give concrete examples because of what I am assuming is variety in data, however). I run the application, yet for some reason, the text I am sending doesn't seem to work, or show up. Anyone know what might be causing this?

Here is my class:

public class MainActivity extends Activity
{
// Creates a URI based on a base URI and a crecord ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";
// Declares a path string for the URIs that you use to copy data
private static final String COPY_PATH = "/copy";
// Declares the URI to paste to the clipboard
Uri copyURI;

// Declares a MIME type constant to match against the MIME types offered by the provider
private static final String MIME_TYPE_CONTACT = "vnd.android.cursor.item/vnd.example.contact";

private int pasteType;
private Menu menu;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    String lastName = "Nichols";
    copyURI = Uri.parse(CONTACTS + COPY_PATH + "/" + lastName);
    setContentView(R.layout.activity_main);
    main();
}

void main()
{
    String text = "Hi, bob!";
    copyText(text);
    String data = paste();
    TextView tv = new TextView(this);
    tv.setText(data);
    setContentView(tv);
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    this.menu = menu;
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@TargetApi(11)
void copyIntent()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    Intent appIntent = new Intent(this, com.example.clipboard.MainActivity.class);
    ClipData clip = ClipData.newIntent("Intent", appIntent);
    clipboard.setPrimaryClip(clip);
    pasteType = 1;
}

@TargetApi(11)
void copyText(String text)
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("simple", text);
    clipboard.setPrimaryClip(clip);
    pasteType = 2;
}

@TargetApi(11)
void copyURI()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newUri(getContentResolver(), "URI", copyURI);
    clipboard.setPrimaryClip(clip);
    pasteType = 3;
}

@TargetApi(11)
String paste()
{
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    /*if (pasteType == 1)
    {
        // Checks to see if the clip item contains an Intent, by testing to see if getIntent() returns null
        Intent pasteIntent = clipboard.getPrimaryClip().getItemAt(0).getIntent();
        if (pasteIntent != null)
        {

        }
    }*/
    if (pasteType == 2)
    {
        String pasteData = "";

        // Gets the ID of the "paste" menu item
        MenuItem mPasteItem = this.menu.findItem(R.id.paste);

        // If the clipboard doesn't conatin data, disable the paste menu item
        if (!(clipboard.hasPrimaryClip()))
            mPasteItem.setEnabled(false);
        else if (!(clipboard.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)))
        {
            // This disables the paste menu item, since the clipboard has data, but it is not plain text
            mPasteItem.setEnabled(false);
        }
        else
            mPasteItem.setEnabled(true);

        // Examines the item on the clipboard. If getText() does not return null, the clip item contains the
        // text. Assume the application can only handle one item at a time.
        ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

        pasteData = (String) item.getText();
        return pasteData;
    }
    /*if (pasteType == 3)
    {

        ContentResolver cr = getContentResolver();
        ClipData clip = clipboard.getPrimaryClip();

        if (clip != null)
        {
            ClipData.Item item = clip.getItemAt(0);
            Uri pasteUri = item.getUri();
            // If the clipboard contains a URI reference
            if (pasteUri != null)
            {
                // Is this a content URI?
                String uriMimeType = cr.getType(pasteUri);
                // If the return value is not null, the Uri is a content URI
                if (uriMimeType != null)
                {
                    // Does the content provider offer a MIME type that the current application can use?
                    if (uriMimeType.equals(MIME_TYPE_CONTACT))
                    {
                        Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);
                        if (pasteCursor != null)
                        {
                            if (pasteCursor.moveToFirst()) {

                            }
                        }
                        pasteCursor.close();
                    }
                }
            }
        }
    }*/
    return null;
}
}

EDIT: Here is my menu.xml file, to help figure out why my menu isn't showing a copy/paste option.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />

</RelativeLayout>
NioShobu
  • 775
  • 2
  • 10
  • 21
  • 1
    Wht do you mean by `the text I am sending doesn't seem to work, or show up`? – iTurki Aug 13 '12 at 18:19
  • I am sending it the text "Hi, bob!" to copy, yet just a typical "Hello world" shows up. – NioShobu Aug 13 '12 at 18:22
  • You mean that `paste()` returns null? And your goal is to copy-and-paste simple text, nothing more? – iTurki Aug 13 '12 at 18:31
  • 1
    Maybe, but I don't see how... Something would have to be wrong with my pasting of the text, though I basically followed it word for word using the guide (though I had to figure some extra things out for `MenuItem mPasteItem = this.menu.findItem(R.id.paste);`... I just realized, I'm not sure it is actually pasting. Checking the menu, the copy and paste options just don't appear at all. I think I might be missing some more. – NioShobu Aug 13 '12 at 18:36
  • Although, should it really matter, since I am calling copy/paste myself? – NioShobu Aug 13 '12 at 18:37
  • 2
    It does matter, the paste option won't show up in your menu until you are in some sort of input view (like EditText). It doesn't show up by SIMPLY calling a method. – iTurki Aug 13 '12 at 18:39
  • Do you know why my copy/paste options might not be appearing? I'll post my menu.xml file. – NioShobu Aug 13 '12 at 18:45
  • If you are implementing them then they won't appear until you select some text. – iTurki Aug 13 '12 at 18:48

1 Answers1

0

I think the guide you are following is a TOTAL misguide! It lacks so much information. However, I suggest you simplify your code:

  • Try to comment some blocks
  • Focus on dealing with text only and forget about the rest for now.

Here is a good answer that might enlighten you.

By the way, your copy() and paste() method are totally fine as far as I can tell. Your problem has something to do with the ContextMenu and how you handle it.

Community
  • 1
  • 1
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • Would this still allow me to test the Clipboard? That's the whole reason for my doing this, and I'm a little confused as to how that link will help me. – NioShobu Aug 13 '12 at 20:12