Example usage for android.view MenuItem setEnabled

List of usage examples for android.view MenuItem setEnabled

Introduction

In this page you can find the example usage for android.view MenuItem setEnabled.

Prototype

public MenuItem setEnabled(boolean enabled);

Source Link

Document

Sets whether the menu item is enabled.

Usage

From source file:com.zen.androidhtmleditor.AHEActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    if (isFinalized)
        ;/*w  w w  . ja v a  2 s .  c  om*/
    //Button backButton = (Button)findViewById(R.id.button2);
    //int vis = backButton.getVisibility();

    MenuItem newfile = menu.findItem(R.id.newfile);
    MenuItem newfolder = menu.findItem(R.id.newfolder);
    MenuItem uploadfile = menu.findItem(R.id.uploadfile);
    MenuItem terminal = menu.findItem(R.id.terminal);

    if (connectedTo > -1) {
        newfile.setEnabled(true);
        newfolder.setEnabled(true);
        uploadfile.setEnabled(true);
        terminal.setEnabled(true);
    } else {
        newfile.setEnabled(false);
        newfolder.setEnabled(false);
        uploadfile.setEnabled(false);
        terminal.setEnabled(false);
    }

    return super.onPrepareOptionsMenu(menu);
}

From source file:org.mozilla.gecko.GeckoApp.java

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    Iterator<ExtraMenuItem> i = sExtraMenuItems.iterator();
    while (i.hasNext()) {
        final ExtraMenuItem item = i.next();
        if (aMenu.findItem(item.id) == null) {
            final MenuItem mi = aMenu.add(Menu.NONE, item.id, Menu.NONE, item.label);
            if (item.icon != null) {
                if (item.icon.startsWith("data")) {
                    byte[] raw = GeckoAppShell.decodeBase64(item.icon.substring(22),
                            GeckoAppShell.BASE64_DEFAULT);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(raw, 0, raw.length);
                    BitmapDrawable drawable = new BitmapDrawable(bitmap);
                    mi.setIcon(drawable);
                } else if (item.icon.startsWith("jar:") || item.icon.startsWith("file://")) {
                    GeckoAppShell.getHandler().post(new Runnable() {
                        public void run() {
                            try {
                                URL url = new URL(item.icon);
                                InputStream is = (InputStream) url.getContent();
                                Drawable drawable = Drawable.createFromStream(is, "src");
                                mi.setIcon(drawable);
                            } catch (Exception e) {
                                Log.w(LOGTAG, "onPrepareOptionsMenu: Unable to set icon", e);
                            }/*w ww .  j av  a  2  s . co m*/
                        }
                    });
                }
            }
            mi.setOnMenuItemClickListener(item);
        }
    }

    if (!sIsGeckoReady)
        aMenu.findItem(R.id.settings).setEnabled(false);

    Tab tab = Tabs.getInstance().getSelectedTab();
    MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    MenuItem forward = aMenu.findItem(R.id.forward);
    MenuItem share = aMenu.findItem(R.id.share);
    MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);

    if (tab == null) {
        bookmark.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        saveAsPDF.setEnabled(false);
        return true;
    }

    bookmark.setEnabled(true);
    bookmark.setCheckable(true);

    if (tab.isBookmark()) {
        bookmark.setChecked(true);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_remove);
    } else {
        bookmark.setChecked(false);
        bookmark.setIcon(R.drawable.ic_menu_bookmark_add);
    }

    forward.setEnabled(tab.canDoForward());

    // Disable share menuitem for about:, chrome: and file: URIs
    String scheme = Uri.parse(tab.getURL()).getScheme();
    boolean enabled = scheme != null
            && !(scheme.equals("about") || scheme.equals("chrome") || scheme.equals("file"));
    share.setEnabled(enabled);

    // Disable save as PDF for about:home and xul pages
    saveAsPDF.setEnabled(!(tab.getURL().equals("about:home")
            || tab.getContentType().equals("application/vnd.mozilla.xul+xml")));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    return true;
}

From source file:de.vanita5.twittnuker.util.Utils.java

public static void setMenuItemAvailability(final Menu menu, final int id, final boolean available) {
    if (menu == null)
        return;/*from w w  w .ja  v a2 s.  com*/
    final MenuItem item = menu.findItem(id);
    if (item == null)
        return;
    item.setVisible(available);
    item.setEnabled(available);
}

From source file:com.smc.tw.waltz.MainActivity.java

private void resetLayout() {
    if (DEBUG)//from  w ww .jav  a 2 s. c  om
        Log.d(TAG, "resetLayout");

    MenuItem menuDropbox = mNavigationView.getMenu().findItem(R.id.navigation_dropbox);
    MenuItem menuDeviceList = mNavigationView.getMenu().findItem(R.id.navigation_device_list);
    MenuItem menuSignin = mNavigationView.getMenu().findItem(R.id.navigation_signin);
    MenuItem menuSignout = mNavigationView.getMenu().findItem(R.id.navigation_signout);

    View headerLayout = mNavigationView.getHeaderView(0);
    TextView userInfoView = (TextView) headerLayout.findViewById(R.id.navigation_header_user_name);

    if (MainApplication.isUserSignedIn()) {
        if (menuSignin != null) {
            menuSignin.setEnabled(false);
            menuSignin.setVisible(false);
        }

        if (menuSignout != null) {
            menuSignout.setEnabled(true);
            menuSignout.setVisible(true);
        }

        if (menuDropbox != null) {
            menuDropbox.setEnabled(true);
            menuDropbox.setVisible(true);
        }

        UserManager userManager = MainApplication.getUserManager();

        String name = userManager.getUsername();

        userInfoView.setText(name);

        userInfoView.setVisibility(View.VISIBLE);
    } else {
        if (menuSignin != null) {
            menuSignin.setEnabled(true);
            menuSignin.setVisible(true);
        }

        if (menuSignout != null) {
            menuSignout.setEnabled(false);
            menuSignout.setVisible(false);
        }

        if (menuDropbox != null) {
            menuDropbox.setEnabled(false);
            menuDropbox.setVisible(false);
        }

        userInfoView.setVisibility(View.GONE);
    }

    if (mShowDropbox) {
        showDropboxAssociation();

        if (menuDropbox != null)
            menuDropbox.setChecked(true);
    } else {
        showDeviceList();

        mDeviceListFragment.notifyDataSetChanged();

        if (menuDeviceList != null)
            menuDeviceList.setChecked(true);
    }
}

From source file:net.zorgblub.typhon.fragment.ReadingFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {

    AppCompatActivity activity = (AppCompatActivity) getActivity();

    if (activity == null) {
        return;/* w ww.j av a  2s.co m*/
    }

    MenuItem nightMode = menu.findItem(R.id.profile_night);
    MenuItem dayMode = menu.findItem(R.id.profile_day);

    MenuItem tts = menu.findItem(R.id.text_to_speech);
    tts.setEnabled(ttsAvailable);

    activity.getSupportActionBar().show();

    if (config.getColourProfile() == ColourProfile.DAY) {
        dayMode.setVisible(false);
        nightMode.setVisible(true);
    } else {
        dayMode.setVisible(true);
        nightMode.setVisible(false);
    }

    // Only show open file item if we have a file manager installed
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (!PlatformUtil.isIntentAvailable(context, intent)) {
        menu.findItem(R.id.open_file).setVisible(false);
    }

    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

@Override
public void onPrepareOptionsMenu(Menu menu) {

    SherlockFragmentActivity activity = getSherlockActivity();

    if (activity == null) {
        return;// www. j a va 2s  .  c om
    }

    MenuItem nightMode = menu.findItem(R.id.profile_night);
    MenuItem dayMode = menu.findItem(R.id.profile_day);

    MenuItem tts = menu.findItem(R.id.text_to_speech);
    tts.setEnabled(ttsAvailable);

    activity.getSupportActionBar().show();

    if (config.getColourProfile() == ColourProfile.DAY) {
        dayMode.setVisible(false);
        nightMode.setVisible(true);
    } else {
        dayMode.setVisible(true);
        nightMode.setVisible(false);
    }

    // Only show open file item if we have a file manager installed
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (!isIntentAvailable(context, intent)) {
        menu.findItem(R.id.open_file).setVisible(false);
    }

    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

From source file:com.nit.vicky.Reviewer.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Resources res = getResources();

    UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_MARK, Menu.NONE, R.string.menu_mark_card,
            R.drawable.ic_menu_mark);//from ww  w .  java2 s .  c om
    UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_UNDO, Menu.NONE, R.string.undo,
            R.drawable.ic_menu_revert_disabled);
    UIUtils.addMenuItem(menu, Menu.NONE, MENU_EDIT, Menu.NONE, R.string.menu_edit_card,
            R.drawable.ic_menu_edit);

    if (mPrefWhiteboard) {
        if (mShowWhiteboard) {
            UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_WHITEBOARD, Menu.NONE,
                    R.string.hide_whiteboard, R.drawable.ic_menu_compose);
        } else {
            UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_WHITEBOARD, Menu.NONE,
                    R.string.show_whiteboard, R.drawable.ic_menu_compose);
        }
        UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_CLEAR_WHITEBOARD, Menu.NONE,
                R.string.clear_whiteboard, R.drawable.ic_menu_clear_playlist);
    }

    SubMenu removeDeckSubMenu = menu.addSubMenu(Menu.NONE, MENU_REMOVE, Menu.NONE, R.string.menu_dismiss_note);
    removeDeckSubMenu.setIcon(R.drawable.ic_menu_stop);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_BURY_CARD, Menu.NONE, R.string.menu_bury_card);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_BURY_NOTE, Menu.NONE, R.string.menu_bury_note);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_SUSPEND_CARD, Menu.NONE, R.string.menu_suspend_card);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_SUSPEND_NOTE, Menu.NONE, R.string.menu_suspend_note);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_DELETE, Menu.NONE, R.string.menu_delete_note);
    if (mPrefTextSelection) {
        MenuItem item = menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, res.getString(R.string.menu_select));
        item.setIcon(R.drawable.ic_menu_search);
        item.setEnabled(Lookup.isAvailable());
    }
    return true;
}

From source file:com.ichi2.anki2.Reviewer.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Resources res = getResources();

    UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_MARK, Menu.NONE, R.string.menu_mark_card,
            R.drawable.ic_menu_mark);/*from   w  ww  .j  a  v a 2 s  .  c  o m*/
    UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_UNDO, Menu.NONE, R.string.undo,
            R.drawable.ic_menu_revert_disabled);
    UIUtils.addMenuItem(menu, Menu.NONE, MENU_EDIT, Menu.NONE, R.string.menu_edit_card,
            R.drawable.ic_menu_edit);
    if (mPrefWhiteboard) {
        if (mShowWhiteboard) {
            UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_WHITEBOARD, Menu.NONE,
                    R.string.hide_whiteboard, R.drawable.ic_menu_compose);
        } else {
            UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_WHITEBOARD, Menu.NONE,
                    R.string.show_whiteboard, R.drawable.ic_menu_compose);
        }
        UIUtils.addMenuItemInActionBar(menu, Menu.NONE, MENU_CLEAR_WHITEBOARD, Menu.NONE,
                R.string.clear_whiteboard, R.drawable.ic_menu_clear_playlist);
    }

    if (mPrefRecord) {
        // TODO proper icon
        final MenuItem recorderItem = UIUtils.addMenuItem(menu, Menu.NONE, MENU_RECORD, Menu.NONE,
                Recorder.recording() ? R.string.record_stop : R.string.record_start,
                R.drawable.ic_circle_pressed);
        final MenuItem replayItem = UIUtils.addMenuItem(menu, Menu.NONE, MENU_RECORD_REPLAY, Menu.NONE,
                R.string.record_play, R.drawable.ic_circle_normal);
        // disable unless something is recorded
        replayItem.setEnabled(false);
        // set relevant listeners
        Recorder.setRecordingStartedListener(new Recorder.RecordingStartedListener() {

            @Override
            public void onRecordingStarted() {
                recorderItem.setTitle(R.string.record_stop);
            }
        });
        Recorder.setRecordingFinishedListener(new Recorder.RecordingFinishedListener() {

            @Override
            public void onRecordingFinished() {
                recorderItem.setTitle(R.string.record_start);
                replayItem.setEnabled(true);
            }
        });
        Recorder.setRecorderResetListener(new Recorder.RecorderResetListener() {

            @Override
            public void onRecorderReset() {
                replayItem.setEnabled(false);
            }
        });
    }

    SubMenu removeDeckSubMenu = menu.addSubMenu(Menu.NONE, MENU_REMOVE, Menu.NONE, R.string.menu_dismiss_note);
    removeDeckSubMenu.setIcon(R.drawable.ic_menu_stop);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_BURY, Menu.NONE, R.string.menu_bury_note);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_SUSPEND_CARD, Menu.NONE, R.string.menu_suspend_card);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_SUSPEND_NOTE, Menu.NONE, R.string.menu_suspend_note);
    removeDeckSubMenu.add(Menu.NONE, MENU_REMOVE_DELETE, Menu.NONE, R.string.menu_delete_note);
    if (mPrefTextSelection) {
        MenuItem item = menu.add(Menu.NONE, MENU_SEARCH, Menu.NONE, res.getString(R.string.menu_select));
        item.setIcon(R.drawable.ic_menu_search);
        item.setEnabled(Lookup.isAvailable());
    }
    return true;
}

From source file:com.ichi2.anki2.Reviewer.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    Resources res = getResources();
    MenuItem item = menu.findItem(MENU_MARK);
    if (mCurrentCard != null && mCurrentCard.note().hasTag("marked")) {
        item.setTitle(R.string.menu_unmark_card);
        item.setIcon(R.drawable.ic_menu_marked);
    } else {/*  w  w  w.jav a2 s .  co  m*/
        item.setTitle(R.string.menu_mark_card);
        item.setIcon(R.drawable.ic_menu_mark);
    }
    item = menu.findItem(MENU_UNDO);
    if (AnkiDroidApp.colIsOpen() && AnkiDroidApp.getCol().undoAvailable()) {
        item.setEnabled(true);
        item.setIcon(R.drawable.ic_menu_revert);
    } else {
        item.setEnabled(false);
        item.setIcon(R.drawable.ic_menu_revert_disabled);
    }
    item = menu.findItem(MENU_SEARCH);
    if (item != null) {
        item.setTitle(clipboardHasText() ? Lookup.getSearchStringTitle() : res.getString(R.string.menu_select));
    }
    return true;
}

From source file:com.seekon.yougouhui.activity.shop.RegisterShopActivity.java

private void registerShop(final MenuItem item) {
    nameView.setError(null);/* www  .  j  ava 2 s .  c o m*/
    addrView.setError(null);
    descView.setError(null);
    pwdView.setError(null);
    pwdConfView.setError(null);
    userNameView.setError(null);

    View focusView = null;
    boolean cancel = false;
    String requiredError = getString(R.string.error_field_required);

    final String password = pwdView.getText().toString();
    final String passwordConf = pwdConfView.getText().toString();
    if (TextUtils.isEmpty(passwordConf)) {
        pwdConfView.setError(requiredError);
        focusView = pwdConfView;
        cancel = true;
    } else if (passwordConf.length() < 4) {
        pwdConfView.setError(getString(R.string.error_invalid_password));
        focusView = pwdConfView;
        cancel = true;
    }
    if (!password.equals(passwordConf)) {
        pwdConfView.setError(getString(R.string.error_password_inconfirmed));
        focusView = pwdConfView;
        cancel = true;
    }
    if (TextUtils.isEmpty(password)) {
        pwdView.setError(requiredError);
        focusView = pwdView;
        cancel = true;
    } else if (password.length() < 4) {
        pwdView.setError(getString(R.string.error_invalid_password));
        focusView = pwdView;
        cancel = true;
    }

    final String userPhone = userPhoneView.getText().toString();
    if (TextUtils.isEmpty(userPhone)) {
        userPhoneView.setError(requiredError);
        focusView = userPhoneView;
        cancel = true;
    }

    final String userName = userNameView.getText().toString();
    if (TextUtils.isEmpty(userName)) {
        userNameView.setError(requiredError);
        focusView = userNameView;
        cancel = true;
    }

    final String desc = descView.getText().toString();
    if (TextUtils.isEmpty(desc)) {
        descView.setError(requiredError);
        focusView = descView;
        cancel = true;
    }
    final String address = addrView.getText().toString();
    if (TextUtils.isEmpty(address)) {
        addrView.setError(requiredError);
        focusView = addrView;
        cancel = true;
    }
    final String name = nameView.getText().toString();
    if (TextUtils.isEmpty(name)) {
        nameView.setError(requiredError);
        focusView = nameView;
        cancel = true;
    }

    if (cancel) {
        focusView.requestFocus();
        if (focusView.equals(pwdView) || focusView.equals(pwdConfView)) {
            viewPager.setCurrentItem(2);
        } else {
            viewPager.setCurrentItem(1);
        }
        return;
    }

    if (shopImage == null) {
        ViewUtils.showToast(".");
        viewPager.setCurrentItem(1);
        return;
    }
    //TODO:??
    //      if (busiLicense == null) {
    //         ViewUtils.showToast("?.");
    //         viewPager.setCurrentItem(1);
    //         return;
    //      }
    if (selectedTrades == null || selectedTrades.isEmpty()) {
        ViewUtils.showToast("?.");
        viewPager.setCurrentItem(1);
        return;
    }

    item.setEnabled(false);

    RestUtils.executeAsyncRestTask(this, new AbstractRestTaskCallback<JSONObjResource>(".") {

        @Override
        public RestMethodResult<JSONObjResource> doInBackground() {
            ShopEntity shop = new ShopEntity();
            shop.setAddress(address);
            shop.setName(name);
            shop.setDesc(desc);
            shop.setShopImage(shopImage);
            shop.setBusiLicense(busiLicense);
            shop.setTrades(selectedTrades);
            shop.setLocation(locationEntity);

            UserEntity currentUser = RunEnv.getInstance().getUser();
            if (UserUtils.isAnonymousUser()) {
                UserEntity emp = new UserEntity();
                emp.setPhone(userPhone);
                emp.setName(userName);
                emp.setPwd(password);
                shop.addEmployee(emp);
            } else {
                shop.setOwner(currentUser.getUuid());
                UserEntity emp = currentUser.clone();
                if (emp != null) {
                    emp.setPwd(password);
                    shop.addEmployee(emp);
                }
            }
            return ShopProcessor.getInstance(RegisterShopActivity.this).registerShop(shop);
        }

        @Override
        public void onSuccess(RestMethodResult<JSONObjResource> result) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }

        @Override
        public void onFailed(String errorMessage) {
            onCancelled();
            super.onFailed(errorMessage);
        }

        @Override
        public void onCancelled() {
            item.setEnabled(true);
        }
    });
}