0

I'm following the guide for Copy and Paste on the Android Developer's page. However, there's a section I don't quite get, which is the section on pasting with the plain text:

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

// If the clipboard doesn't contain data, disable the paste menu item.
// If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {

mPasteItem.setEnabled(false);

} else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {

    // This disables the paste menu item, since the clipboard has data but it is not plain text
    mPasteItem.setEnabled(false);
} else {

    // This enables the paste menu item, since the clipboard contains plain text.
    mPasteItem.setEnabled(true);
}
}

I can understand most of it, but what caught me off guard was the use of a member variable. I know that these guides aren't 1:1 code, but I was following just find and saw no mention of a variable named "menu." So, I'm asking to those who probably know Android more than I do, what is the purpose of this variable? I know that I went back and edited the menu.xml file so that I had a "Copy" and "Paste" item (which this "tutorial" seems to use), but now I don't know how to instantiate/initialize this menu, nor do I really know its purpose. Can someone explain this to me?

Thanks.

NioShobu
  • 775
  • 2
  • 10
  • 21

1 Answers1

2

Silly Android developers, using undocumented variables...

That's a reference to some layout containing the Menu in which the paste button (a MenuItem) can be found. This item has ID menu_paste, which you may or may not already know.

Realistically, this menu variable can be any Menu layout that contains your menu item. You can find out how to create Menus here.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • I have a menu made, how do I then initialize this "menu" variable? – NioShobu Aug 10 '12 at 22:57
  • It depends on how you create the menu. See, in some event where, let's say, you are setting up an [Action Bar or options menu](http://developer.android.com/guide/topics/ui/menus.html#options-menu), your code would go in the `onCreateOptionsMenu(...)` method. There, the `menu` item already exists and that is the one you want to be using. – Cat Aug 10 '12 at 22:59
  • I have `getMenuInflater().inflate(R.menu.activity_main, menu); return true;` in that method, which sends in a Menu. Should I just sent in a menu to my pasteText() method? – NioShobu Aug 10 '12 at 23:04
  • Yes; you could even save the `menu` here (to a member variable), assuming it's the one that contains your `menu_paste` item. – Cat Aug 10 '12 at 23:05
  • Like this: `this.menu = menu`? – NioShobu Aug 10 '12 at 23:10
  • Now I am encountering this: `clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN` . I think MIMETYPE_TEXT_PLAIN must be a global variable for my class, but what would it be? – NioShobu Aug 10 '12 at 23:15
  • 1
    Use `ClipDescription.MIMETYPE_TEXT_PLAIN`. – Cat Aug 10 '12 at 23:21
  • I've got another one: `pasteData = resolveUri(Uri);`. I'm being told I can't use just Uri (it's not a resolved variable), but that is all the guide says. I even tried Uri.class. – NioShobu Aug 10 '12 at 23:33
  • `resolveUri(Uri)` should be `resolveUri(pasteUri)`. The devs didn't do a good job of debugging this one. – Cat Aug 10 '12 at 23:37
  • This is not a method contained in my class, and the guide does not mention anything about it. Do you know of a class that might contain this method, or if it is really all that necessary? (I can provide the code for that last part) – NioShobu Aug 13 '12 at 16:24
  • 1
    Ah, kay, that's a method they made. You don't even need it, actually; you're only copying text, so you don't need to keep anything after `pasteData = item.getText();`. – Cat Aug 13 '12 at 17:22
  • I've finished the most I can from the basic guide (filling in several holes to the best I could, while also cutting out the pasting for anything other than just text, yet now my Android app doesn't show up. Apparently its installed, but it doesn't pop up automatically (I'm using Eclipse with ADT) and the app doesn't show up with the rest of my apps. – NioShobu Aug 13 '12 at 17:53
  • Start a new question with all your code, and explain the problem in detail. It would also help if you removed bits of code at a time until you figured out which one was preventing the app from appearing. (This sounds like a bad install, not bad code, though.) – Cat Aug 13 '12 at 17:56