Android Menu Dynamic Change "Add or remove"

Source: Internet
Author: User

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android">    <GroupAndroid:id= "@+id/menu_items">        <ItemAndroid:id= "@+id/menu_item_night_mode"Android:title= "@string/menu_item_night_mode"Android:icon= "@android:d rawable/ic_menu_preferences"/>        <ItemAndroid:id= "@+id/menu_item_settings"Android:title= "@string/menu_item_settings"Android:icon= "@android:d rawable/ic_menu_preferences"/>        <ItemAndroid:id= "@+id/menu_item_help"Android:title= "@string/menu_item_help"Android:icon= "@android:d rawable/ic_menu_preferences"/>    </Group></Menu>
@Override Public BooleanOncreateoptionsmenu (Menu menu) {//We only have want to show it as a menu in landscape, and only for clock/alarm fragment.Mmenu =menu; //Direction Level        if(Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) {            if(Mactionbar.getselectednavigationindex () = = Alarm_tab_index | |Mactionbar.getselectednavigationindex ()==Clock_tab_index) {                //Clear The menu so it doesn ' t get duplicate items in case Oncreateoptionsmenu//Was called multiple times.menu.clear ();                Getmenuinflater (). Inflate (R.menu.desk_clock_menu, menu); MenuItem Nightmode=Menu.finditem (R.id.menu_item_night_mode); MenuItem Help=Menu.finditem (R.ID.MENU_ITEM_HELP); if(Mactionbar.getselectednavigationindex () = =Alarm_tab_index) {nightmode.setvisible (false); Help.setvisible (false); } Else if(Mactionbar.getselectednavigationindex () = =Clock_tab_index) {nightmode.setvisible (true); Help.setvisible (false); }            }            //Always return true to landscape, regardless of whether we ve inflated the menu, so//That's when we switch tabs This method would get called and we can inflate the menu.            return true; }        return false; } @Override Public BooleanOnprepareoptionsmenu (Menu menu) {updatemenu (menu); return true; }    Private voidUpdatemenu (Menu menu) {//Hide "Help" If we don has a URI for it.MenuItem help =Menu.finditem (R.ID.MENU_ITEM_HELP); if(Help! =NULL) {Utils.preparehelpmenuitem ( This, help); }        //Hide ' Lights Out ' for timer.MenuItem Nightmode =Menu.finditem (R.id.menu_item_night_mode); if(Mactionbar.getselectednavigationindex () = =Alarm_tab_index) {nightmode.setvisible (false); } Else if(Mactionbar.getselectednavigationindex () = =Clock_tab_index) {nightmode.setvisible (true); }} @Override Public Booleanonoptionsitemselected (MenuItem item) {if(Processmenuclick (item)) {return true; }        return Super. onoptionsitemselected (item); }    Private BooleanProcessmenuclick (MenuItem item) {Switch(Item.getitemid ()) { Caser.id.menu_item_settings:startactivity (NewIntent (Deskclock. This, Settingsactivity.class)); return true;  Caser.id.menu_item_help:intent i=item.getintent (); if(I! =NULL) {                    Try{startactivity (i); } Catch(activitynotfoundexception e) {//No activity found to match the Intent-ignore                    }                }                return true;  Caser.id.menu_item_night_mode:startactivity (NewIntent (Deskclock. This, Screensaveractivity.class)); default:                 Break; }        return true; }

Android Menu Dynamic Change "Add or remove"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.