Example usage for android.view MenuItem setTitle

List of usage examples for android.view MenuItem setTitle

Introduction

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

Prototype


public MenuItem setTitle(@StringRes int title);

Source Link

Document

Change the title associated with this item.

Usage

From source file:it.angrydroids.epub3reader.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.FirstEPUB:
        bookSelector = 0;//  www  .  j  a va  2 s .c o m
        Intent goToChooser1 = new Intent(this, FileChooser.class);
        goToChooser1.putExtra(getString(R.string.second), getString(R.string.time));
        startActivityForResult(goToChooser1, 0);
        return true;

    case R.id.SecondEPUB:
        bookSelector = 1;
        Intent goToChooser2 = new Intent(this, FileChooser.class);
        goToChooser2.putExtra(getString(R.string.second), getString(R.string.time));
        startActivityForResult(goToChooser2, 0);
        return true;

    case R.id.Front:
        if (navigator.exactlyOneBookOpen() == true || navigator.isParallelTextOn() == true)
            chooseLanguage(0);
        return true;

    case R.id.FirstFront:
        chooseLanguage(0);
        return true;

    case R.id.SecondFront:
        if (navigator.exactlyOneBookOpen() == false)
            chooseLanguage(1);
        else
            errorMessage(getString(R.string.error_onlyOneBookOpen));
        return true;

    case R.id.PconS:
        try {
            boolean yes = navigator.synchronizeView(1, 0);
            if (!yes) {
                errorMessage(getString(R.string.error_onlyOneBookOpen));
            }
        } catch (Exception e) {
            errorMessage(getString(R.string.error_cannotSynchronize));
        }
        return true;

    case R.id.SconP:
        try {
            boolean ok = navigator.synchronizeView(0, 1);
            if (!ok) {
                errorMessage(getString(R.string.error_onlyOneBookOpen));
            }
        } catch (Exception e) {
            errorMessage(getString(R.string.error_cannotSynchronize));
        }
        return true;

    case R.id.Synchronize:

        boolean sync = navigator.flipSynchronizedReadingActive();
        if (!sync) {
            errorMessage(getString(R.string.error_onlyOneBookOpen));
        }
        return true;

    case R.id.Metadata:
        if (navigator.exactlyOneBookOpen() == true || navigator.isParallelTextOn() == true) {
            navigator.displayMetadata(0);
        } else {
        }
        return true;

    case R.id.meta1:
        if (!navigator.displayMetadata(0))
            errorMessage(getString(R.string.error_metadataNotFound));
        return true;

    case R.id.meta2:
        if (!navigator.displayMetadata(1))
            errorMessage(getString(R.string.error_metadataNotFound));
        return true;

    case R.id.tableOfContents:
        if (navigator.exactlyOneBookOpen() == true || navigator.isParallelTextOn() == true)
            navigator.displayTOC(0);
        return true;

    case R.id.toc1:
        if (!navigator.displayTOC(0))
            errorMessage(getString(R.string.error_tocNotFound));
        return true;
    case R.id.toc2:
        if (navigator.displayTOC(1))
            errorMessage(getString(R.string.error_tocNotFound));
        return true;
    case R.id.changeSize:
        try {
            DialogFragment newFragment = new SetPanelSize();
            newFragment.show(getFragmentManager(), "");
        } catch (Exception e) {
            errorMessage(getString(R.string.error_cannotChangeSizes));
        }
        return true;
    case R.id.Style: // work in progress...
        try {
            if (navigator.exactlyOneBookOpen() == true) {
                DialogFragment newFragment = new ChangeCSSMenu();
                newFragment.show(getFragmentManager(), "");
                bookSelector = 0;
            }
        } catch (Exception e) {
            errorMessage(getString(R.string.error_CannotChangeStyle));
        }
        return true;

    case R.id.StyleBook1:
        try {
            {
                DialogFragment newFragment = new ChangeCSSMenu();
                newFragment.show(getFragmentManager(), "");
                bookSelector = 0;
            }
        } catch (Exception e) {
            errorMessage(getString(R.string.error_CannotChangeStyle));
        }
        return true;

    case R.id.StyleBook2:
        try {
            {
                DialogFragment newFragment = new ChangeCSSMenu();
                newFragment.show(getFragmentManager(), "");
                bookSelector = 1;
            }
        } catch (Exception e) {
            errorMessage(getString(R.string.error_CannotChangeStyle));
        }
        return true;

    /*
     * case R.id.SyncScroll: syncScrollActivated = !syncScrollActivated;
     * return true;
     */

    case R.id.audio:
        if (navigator.exactlyOneBookOpen() == true)
            if (!navigator.extractAudio(0))
                errorMessage(getString(R.string.no_audio));
        return true;
    case R.id.firstAudio:
        if (!navigator.extractAudio(0))
            errorMessage(getString(R.string.no_audio));
        return true;
    case R.id.secondAudio:
        if (!navigator.extractAudio(1))
            errorMessage(getString(R.string.no_audio));
        return true;
    case R.id.ScanBLE:
        Log.i(TAG, "Scanning button is pressed");
        // auto scan and attach BLE device here.
        switch (mBLEState) {
        case UART_PROFILE_DISCONNECTED:
            Toast.makeText(this, "Scanning for BLE devices", Toast.LENGTH_SHORT).show();
            scanBLEDevices();
            item.setTitle("Scanning...");
            break;
        case UART_PROFILE_CONNECTED:
            if (mDevice != null) {
                mService.disconnect();
            }
            break;
        case UART_SCANNING_BLE_DEVICES:
            Toast.makeText(this, "Scanning for BLE devices", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
        // If success, change label to Disconnect BLE and notify a successful connection
        // Otherwise, keep the name and show info message
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

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

@Override
public boolean onPrepareOptionsMenu(Menu aMenu) {
    if (aMenu == null)
        return false;

    // Hide the tab history panel when hardware menu button is pressed.
    TabHistoryFragment frag = (TabHistoryFragment) getSupportFragmentManager()
            .findFragmentByTag(TAB_HISTORY_FRAGMENT_TAG);
    if (frag != null) {
        frag.dismiss();/*from   www.ja  v a  2  s . c  om*/
    }

    if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
        aMenu.findItem(R.id.settings).setEnabled(false);
        aMenu.findItem(R.id.help).setEnabled(false);
    }

    Tab tab = Tabs.getInstance().getSelectedTab();
    final MenuItem bookmark = aMenu.findItem(R.id.bookmark);
    final MenuItem reader = aMenu.findItem(R.id.reading_list);
    final MenuItem back = aMenu.findItem(R.id.back);
    final MenuItem forward = aMenu.findItem(R.id.forward);
    final MenuItem share = aMenu.findItem(R.id.share);
    final MenuItem quickShare = aMenu.findItem(R.id.quickshare);
    final MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf);
    final MenuItem charEncoding = aMenu.findItem(R.id.char_encoding);
    final MenuItem findInPage = aMenu.findItem(R.id.find_in_page);
    final MenuItem desktopMode = aMenu.findItem(R.id.desktop_mode);
    final MenuItem enterGuestMode = aMenu.findItem(R.id.new_guest_session);
    final MenuItem exitGuestMode = aMenu.findItem(R.id.exit_guest_session);

    // Only show the "Quit" menu item on pre-ICS, television devices,
    // or if the user has explicitly enabled the clear on shutdown pref.
    // (We check the pref last to save the pref read.)
    // In ICS+, it's easy to kill an app through the task switcher.
    final boolean visible = Versions.preICS || HardwareUtils.isTelevision() || !PrefUtils
            .getStringSet(GeckoSharedPrefs.forProfile(this), ClearOnShutdownPref.PREF, new HashSet<String>())
            .isEmpty();
    aMenu.findItem(R.id.quit).setVisible(visible);
    aMenu.findItem(R.id.logins).setVisible(AppConstants.NIGHTLY_BUILD);

    if (tab == null || tab.getURL() == null) {
        bookmark.setEnabled(false);
        reader.setEnabled(false);
        back.setEnabled(false);
        forward.setEnabled(false);
        share.setEnabled(false);
        quickShare.setEnabled(false);
        saveAsPDF.setEnabled(false);
        findInPage.setEnabled(false);

        // NOTE: Use MenuUtils.safeSetEnabled because some actions might
        // be on the BrowserToolbar context menu.
        if (Versions.feature11Plus) {
            // There is no page menu prior to v11 resources.
            MenuUtils.safeSetEnabled(aMenu, R.id.page, false);
        }
        MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, false);
        MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, false);

        return true;
    }

    final boolean inGuestMode = GeckoProfile.get(this).inGuestMode();

    final boolean isAboutReader = AboutPages.isAboutReader(tab.getURL());
    bookmark.setEnabled(!isAboutReader);
    bookmark.setVisible(!inGuestMode);
    bookmark.setCheckable(true);
    bookmark.setChecked(tab.isBookmark());
    bookmark.setIcon(resolveBookmarkIconID(tab.isBookmark()));
    bookmark.setTitle(resolveBookmarkTitleID(tab.isBookmark()));

    reader.setEnabled(isAboutReader || !AboutPages.isAboutPage(tab.getURL()));
    reader.setVisible(!inGuestMode);
    reader.setCheckable(true);
    final boolean isPageInReadingList = tab.isInReadingList();
    reader.setChecked(isPageInReadingList);
    reader.setIcon(resolveReadingListIconID(isPageInReadingList));
    reader.setTitle(resolveReadingListTitleID(isPageInReadingList));

    back.setEnabled(tab.canDoBack());
    forward.setEnabled(tab.canDoForward());
    desktopMode.setChecked(tab.getDesktopMode());
    desktopMode.setIcon(
            tab.getDesktopMode() ? R.drawable.ic_menu_desktop_mode_on : R.drawable.ic_menu_desktop_mode_off);

    View backButtonView = MenuItemCompat.getActionView(back);

    if (backButtonView != null) {
        backButtonView.setOnLongClickListener(new Button.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Tab tab = Tabs.getInstance().getSelectedTab();
                if (tab != null) {
                    closeOptionsMenu();
                    return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.BACK);
                }
                return false;
            }
        });
    }

    View forwardButtonView = MenuItemCompat.getActionView(forward);

    if (forwardButtonView != null) {
        forwardButtonView.setOnLongClickListener(new Button.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Tab tab = Tabs.getInstance().getSelectedTab();
                if (tab != null) {
                    closeOptionsMenu();
                    return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.FORWARD);
                }
                return false;
            }
        });
    }

    String url = tab.getURL();
    if (AboutPages.isAboutReader(url)) {
        String urlFromReader = ReaderModeUtils.getUrlFromAboutReader(url);
        if (urlFromReader != null) {
            url = urlFromReader;
        }
    }

    // Disable share menuitem for about:, chrome:, file:, and resource: URIs
    final boolean shareVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_SHARE);
    share.setVisible(shareVisible);
    final boolean shareEnabled = StringUtils.isShareableUrl(url) && shareVisible;
    share.setEnabled(shareEnabled);
    MenuUtils.safeSetEnabled(aMenu, R.id.downloads,
            RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_DOWNLOADS));

    // NOTE: Use MenuUtils.safeSetEnabled because some actions might
    // be on the BrowserToolbar context menu.
    if (Versions.feature11Plus) {
        MenuUtils.safeSetEnabled(aMenu, R.id.page, !isAboutHome(tab));
    }
    MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, tab.hasFeeds());
    MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, tab.hasOpenSearch());
    MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, !isAboutHome(tab));
    MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, !isAboutHome(tab));

    // Action providers are available only ICS+.
    if (Versions.feature14Plus) {
        quickShare.setVisible(shareVisible);
        quickShare.setEnabled(shareEnabled);

        // This provider also applies to the quick share menu item.
        final GeckoActionProvider provider = ((GeckoMenuItem) share).getGeckoActionProvider();
        if (provider != null) {
            Intent shareIntent = provider.getIntent();

            // For efficiency, the provider's intent is only set once
            if (shareIntent == null) {
                shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                provider.setIntent(shareIntent);
            }

            // Replace the existing intent's extras
            shareIntent.putExtra(Intent.EXTRA_TEXT, url);
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, tab.getDisplayTitle());
            shareIntent.putExtra(Intent.EXTRA_TITLE, tab.getDisplayTitle());
            shareIntent.putExtra(ShareDialog.INTENT_EXTRA_DEVICES_ONLY, true);

            // Clear the existing thumbnail extras so we don't share an old thumbnail.
            shareIntent.removeExtra("share_screenshot_uri");

            // Include the thumbnail of the page being shared.
            BitmapDrawable drawable = tab.getThumbnail();
            if (drawable != null) {
                Bitmap thumbnail = drawable.getBitmap();

                // Kobo uses a custom intent extra for sharing thumbnails.
                if (Build.MANUFACTURER.equals("Kobo") && thumbnail != null) {
                    File cacheDir = getExternalCacheDir();

                    if (cacheDir != null) {
                        File outFile = new File(cacheDir, "thumbnail.png");

                        try {
                            java.io.FileOutputStream out = new java.io.FileOutputStream(outFile);
                            thumbnail.compress(Bitmap.CompressFormat.PNG, 90, out);
                        } catch (FileNotFoundException e) {
                            Log.e(LOGTAG, "File not found", e);
                        }

                        shareIntent.putExtra("share_screenshot_uri", Uri.parse(outFile.getPath()));
                    }
                }
            }
        }
    }

    final boolean privateTabVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_PRIVATE_BROWSING);
    MenuUtils.safeSetVisible(aMenu, R.id.new_private_tab, privateTabVisible);

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

    // Disable find in page for about:home, since it won't work on Java content.
    findInPage.setEnabled(!isAboutHome(tab));

    charEncoding.setVisible(GeckoPreferences.getCharEncodingState());

    if (mProfile.inGuestMode()) {
        exitGuestMode.setVisible(true);
    } else {
        enterGuestMode.setVisible(true);
    }

    if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_GUEST_BROWSING)) {
        MenuUtils.safeSetVisible(aMenu, R.id.new_guest_session, false);
    }

    if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_INSTALL_EXTENSION)) {
        MenuUtils.safeSetVisible(aMenu, R.id.addons, false);
    }

    return true;
}

From source file:com.rfo.basic.Run.java

@Override
public boolean onCreateOptionsMenu(Menu menu) { // Called when the menu key is pressed.
    super.onCreateOptionsMenu(menu);
    if (!Settings.getConsoleMenu(this)) {
        return false;
    }//w  ww  . j a v  a2s.c  om

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.run, menu);
    MenuItem item = menu.getItem(1);
    if (Basic.DoAutoRun) { // If APK or shortcut, menu action is "Exit", not "Editor"
        item.setTitle(getString(R.string.exit));
    }
    item.setEnabled(false);
    return true;
}