简体   繁体   中英

How to add a menu item to an existing Android app menu?

I am very new at android development and am having trouble adding a new Menu item to the app's menu and getting it to open and present a menu view layout when tapped. I've so far only been able to change the menu.xml file for the app, which is enough to create a button that sits alongside others which show up when you press the Menu button but not enough to get it to link to anything.

My goal is just to have pressing the button result in it connecting to a simple xml layout or dialogue page. I'm guessing I'll need to change the java code, but I'm not sure what exactly is needed to get this done. I'd appreciate any suggestions.

It seems to end "To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using MenuInflater.inflate(). In the following sections, you'll see how to inflate a menu for each menu type." I guess what I need to know is how to add the item to the inflator for the other items.

Does anyone happen to have a vanilla version of something I can insert?

Here is an example of existing code which opens a dialogue and which deals with a close and "changes" button:

private void openHelpDialog() {
    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.aboutview, null); 
    TextView tv = (TextView)view.findViewById(R.id.aboutVersionCode);
    tv.setText(getVersionName() + " (revision " + getVersionNumber() + ")");
    new AlertDialog.Builder(MainActivity.this)
    .setTitle(getResources().getString(R.string.application_name) + " " + getResources().getString(R.string.menu_help))
    .setIcon(R.drawable.about)
    .setView(view)
    .setNeutralButton(R.string.menu_changes, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          MainActivity.this.openChangesDialog();
      }
    })
    .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      }
    })
    .show();  
}

private void openChangesDialog() {
  LayoutInflater li = LayoutInflater.from(this);
  View view = li.inflate(R.layout.changeview, null); 
  new AlertDialog.Builder(MainActivity.this)
  .setTitle(R.string.changelog_title)
  .setIcon(R.drawable.about)
  .setView(view)
  .setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //
    }
  })
  .show();  
}

private void openClearDialog() {
    new AlertDialog.Builder(MainActivity.this)
    .setTitle(R.string.context_menu_clear_grid_confirmation_title)
    .setMessage(R.string.context_menu_clear_grid_confirmation_message)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setNegativeButton(R.string.context_menu_clear_grid_negative_button_label, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
          //
      }
    })
    .setPositiveButton(R.string.context_menu_clear_grid_positive_button_label, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            MainActivity.this.kenKenGrid.clearUserValues();
        }
    })
    .show();  
  }

You can find many examples from sample projects which is already in your Eclipse. Is this is what are you asking ?

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent serverIntent = null;
    switch (item.getItemId()) {
    case R.id.secure_connect_scan:
        // Launch the DeviceListActivity to see devices and do scan
        serverIntent = new Intent(this, DeviceListActivity.class);
        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE);
        return true;
    case R.id.insecure_connect_scan:
        // Launch the DeviceListActivity to see devices and do scan
        serverIntent = new Intent(this, DeviceListActivity.class);
        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
        return true;
    case R.id.discoverable:
        // Ensure this device is discoverable by others
        ensureDiscoverable();
        return true;
    }
    return false;
}

Actually this code was taken from BluetoothChat example . Hope this one helps .

You can add a submenu programmatically in your Activity something like this. Adapted from a tutorial here :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optionsmenu, menu);

    SubMenu menu4 = menu.addSubMenu(Menu.NONE, MENU4, 4,"Menu No. 4");
    menu4.add(GROUP1, SUBMENU1, 1, "SubMenu No. 1");
    menu4.add(GROUP1, SUBMENU2, 2, "SubMenu No. 2");
    menu4.setGroupCheckable(GROUP1,true,true);

    return true;

}

In xml, you can do it like this (taken from here ):

<item android:title="Normal 1" />

<item android:id="@+id/submenu"
    android:title="Emotions">

    <menu>        

        <item android:id="@+id/happy"
            android:title="Happy"
            android:icon="@drawable/stat_happy" />

        <item android:id="@+id/neutral"
            android:title="Neutral"
            android:icon="@drawable/stat_neutral" />

        <item android:id="@+id/sad"
            android:title="Sad"
            android:icon="@drawable/stat_sad" />

    </menu>

</item>

<item android:title="Normal 2" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM