Example usage for android.view Menu size

List of usage examples for android.view Menu size

Introduction

In this page you can find the example usage for android.view Menu size.

Prototype

public int size();

Source Link

Document

Get the number of items in the menu.

Usage

From source file:com.vuze.android.remote.activity.DrawerActivity.java

public boolean onPrepareOptionsMenu_drawer(Menu menu) {

    if (mDrawerLayout == null) {
        return true;
    }/*from  w w  w  .  j a va  2s .c  om*/

    if (!mDrawerLayout.isDrawerOpen(mDrawerView)) {
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            item.setVisible(true);
        }
    } else {
        final int[] itemsToKeep = { R.id.action_about, R.id.action_logout, R.id.action_social,
                R.id.action_settings };
        for (int i = 0; i < menu.size(); i++) {
            MenuItem item = menu.getItem(i);
            int id = item.getItemId();
            boolean visible = false;
            for (int itemID : itemsToKeep) {
                if (id == itemID) {
                    visible = true;
                    break;
                }
            }
            item.setVisible(visible);
        }
        return false; // skip all other prepares
    }

    return true;
}

From source file:org.dmfs.webcal.NavbarActivity.java

protected void setupNavbar() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // this is a hack to get a Menu
    Menu menu = new PopupMenu(this, null).getMenu();
    getMenuInflater().inflate(R.menu.side_navigation, menu);

    // remove invisible menu items
    int itemCount = menu.size();
    int i = 0;//ww  w . j  a  va 2 s.  co m
    while (i < itemCount) {
        MenuItem item = menu.getItem(i);
        if (!item.isVisible()) {
            menu.removeItem(item.getItemId());
            --itemCount;
        } else {
            ++i;
        }
    }

    MenuAdapter menuAdapter = new MenuAdapter(this, R.layout.navdrawer_item, android.R.id.title, menu);
    mDrawerList.setAdapter(menuAdapter);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            String activityTitle = getActivityTitle();
            setTitle(activityTitle);
            invalidateOptionsMenu();
            super.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            setTitle(R.string.app_name);
            invalidateOptionsMenu();
            super.onDrawerOpened(drawerView);
        }
    };
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(this);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

From source file:com.kccomy.orgar.ui.note.NoteFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.toolbar_note_tree, menu);

    for (int i = 0; i < menu.size(); i++) {
        menu.getItem(i).setOnMenuItemClickListener(this);
    }//from w  w  w  .j a  va 2s .c o  m
}

From source file:com.offsec.nethunter.AppNavHomeActivity.java

@Override
public void onBackPressed() {
    super.onBackPressed();
    if (titles.size() > 1) {
        titles.pop();//from  w  w w .  ja  v a  2 s.  c o m
        mTitle = titles.peek();
    }
    Menu menuNav = navigationView.getMenu();
    int i = 0;
    int mSize = menuNav.size();
    while (i < mSize) {
        if (menuNav.getItem(i).getTitle() == mTitle) {
            MenuItem _current = menuNav.getItem(i);
            if (lastSelected != _current) {
                //remove last
                lastSelected.setChecked(false);
                // udpate for the next
                lastSelected = _current;
            }
            //set checked
            _current.setChecked(true);
            i = mSize;
        }
        i++;
    }
    restoreActionBar();
}

From source file:com.tweetlanes.android.core.view.TweetSpotlightActivity.java

void storeMenuItems(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem menuItem = menu.getItem(i);
        if (menuItem.getItemId() == R.id.action_favorite) {
            mFavoriteMenuItem = menuItem;
            setIsFavorited();/*from w ww.  ja va 2s.c  o  m*/
        }
        if (menuItem.getItemId() == R.id.action_retweet) {
            mRetweetMenuItem = menuItem;
            setIsRetweeted();
        }
    }
}

From source file:com.heske.alexandria.activities.MainActivity.java

private int findNavPosition() {
    Menu menu = mNavView.getMenu();
    int count = menu.size();

    for (int i = 0; i < count; i++) {
        if (menu.getItem(i).isChecked())
            return i;
    }//from ww w.  j a va  2  s.c  o  m
    //Default to BookList
    return 0;
}

From source file:io.tehtotalpwnage.musicphp_android.NavigationActivity.java

private int getCheckedItem(NavigationView navView) {
    Menu menu = navView.getMenu();
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.isChecked()) {
            return i;
        }/*  w  w  w  . j ava 2s.  c o  m*/
    }

    return 0;
}

From source file:cs.man.ac.uk.tavernamobile.fragments.SearchResultFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    // remove menu added by previous fragment
    for (int i = 1; i < menu.size(); i++) {
        menu.removeItem(menu.getItem(i).getItemId());
    }/*from   www  . ja v  a  2  s  . c  o  m*/
    parentActivity.getMenuInflater().inflate(R.menu.search_results_screen, menu);
    LinearLayout searchView = (LinearLayout) menu.findItem(R.id.search_results_search).getActionView();

    final EditText query = (EditText) searchView.getChildAt(0);
    query.requestFocus();
    query.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchQuery = query.getText().toString();
                search(searchQuery);
                return true;
            }
            return false;
        }
    });
    ImageButton searchButton = (ImageButton) searchView.getChildAt(1);

    searchButton.setOnClickListener(new android.view.View.OnClickListener() {
        public void onClick(android.view.View v) {
            searchQuery = query.getText().toString();
            search(searchQuery);
        }
    });
    super.onPrepareOptionsMenu(menu);
}

From source file:com.eugene.fithealthmaingit.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null) {
        mNavItemId = savedInstanceState.getInt(NAV_ITEM_ID);
        isFirstFragmentAdded = savedInstanceState.getBoolean(FIRST_FRAGMENT_ADDED);
    } else {// ww  w.j av a2s . c  om
        mNavItemId = R.id.nav_journal;
    }

    mNavigationDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Set menu header text to User Name
    TextView mHeaderText = (TextView) findViewById(R.id.txtName);
    mHeaderText.setText(PreferenceManager.getDefaultSharedPreferences(this).getString(Globals.USER_NAME, ""));

    /**
     * Initiate NavigationView
     * Inflate Menu based on FitBit connection status
     */
    NavigationView mNavigationView = (NavigationView) findViewById(R.id.nav);

    if (PreferenceManager.getDefaultSharedPreferences(this).getString("FITBIT_ACCESS_TOKEN", "").equals("")) {
        mNavigationView.inflateMenu(R.menu.drawer);
    } else {
        mNavigationView.inflateMenu(R.menu.drawer_fitbit);
    }
    mNavigationView.getMenu().findItem(mNavItemId).setChecked(true);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            if (menuItem.getItemId() != R.id.nav_settings) {
                mNavItemId = menuItem.getItemId();
                switchFragment(menuItem.getItemId());
                menuItem.setChecked(true);
                Fragment loading = new FragmentBlankLoading();
                Bundle b = new Bundle();
                b.putInt(NAV_ITEM_ID, mNavItemId);
                loading.setArguments(b);
                getSupportFragmentManager().beginTransaction().replace(R.id.container, loading).commit();
            } else {
                startActivity(new Intent(MainActivity.this, UserInformationActivity.class));
            }
            handleNavigationDrawer();
            return false;
        }
    });

    // Nav Menu Fonts
    Menu m = mNavigationView.getMenu();
    for (int i = 0; i < m.size(); i++) {
        MenuItem mi = m.getItem(i);

        //for aapplying a font to subMenu ...
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu != null && subMenu.size() > 0) {
            for (int j = 0; j < subMenu.size(); j++) {
                MenuItem subMenuItem = subMenu.getItem(j);
                applyFontToMenuItem(subMenuItem);
            }
        }

        //the method we have create in activity
        applyFontToMenuItem(mi);
    }
    switchFragment(mNavItemId);

    /**
     * Handles Home Screen Widget
     * Search and Add
     */
    Intent widgetIntent = this.getIntent();
    if (widgetIntent != null) {
        if (widgetIntent.getAction() != null && savedInstanceState == null) {
            if (widgetIntent.getAction().equals(FitHealthWidget.ACTION_SEARCH)) {
                // Post delay to allow the app to open and not interfere with animation
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.containerSearch, new FragmentSearch()).addToBackStack(null)
                                .commit();
                    }
                }, 100);
            }
            if (widgetIntent.getAction().equals(FitHealthWidget.ACTION_ADD)) {
                widgetAdd();
            }
        }
    }
}

From source file:com.nextgis.mobile.fragment.AttributesFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == R.id.menu_about || item.getItemId() == R.id.menu_settings) {
            continue;
        }//from w ww .  j  av a2  s  . c o  m
        item.setVisible(false);
    }
    super.onPrepareOptionsMenu(menu);
}