Java Code Examples for android.support.v7.widget.Toolbar#getMenu()

The following examples show how to use android.support.v7.widget.Toolbar#getMenu() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChapterFragment.java    From fingerpoetry-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void initToolbarMenu(Toolbar toolbar) {
    toolbar.inflateMenu(R.menu.menu_article_pop);
    menu = toolbar.getMenu();
    menu.getItem(0).setVisible(false);
    menu.getItem(1).setVisible(false);
}
 
Example 2
Source File: ToolbarContentTintHelper.java    From APlayer with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static void setToolbarContentColor(@NonNull Context context, Toolbar toolbar,
    @Nullable Menu menu, final @ColorInt int toolbarContentColor,
    final @ColorInt int titleTextColor, final @ColorInt int subtitleTextColor,
    final @ColorInt int menuWidgetColor) {
  if (toolbar == null) {
    return;
  }

  if (menu == null) {
    menu = toolbar.getMenu();
  }

  toolbar.setTitleTextColor(titleTextColor);
  toolbar.setSubtitleTextColor(subtitleTextColor);

  if (toolbar.getNavigationIcon() != null) {
    // Tint the toolbar navigation icon (e.g. back, drawer, etc.)
    toolbar.setNavigationIcon(
        TintHelper.createTintedDrawable(toolbar.getNavigationIcon(), toolbarContentColor));
  }

  InternalToolbarContentTintUtil.tintMenu(toolbar, menu, toolbarContentColor);
  InternalToolbarContentTintUtil.applyOverflowMenuTint(context, toolbar, menuWidgetColor);

  if (context instanceof Activity) {
    InternalToolbarContentTintUtil
        .setOverflowButtonColor((Activity) context, toolbarContentColor);
  }

  try {
    // Tint immediate overflow menu items
    final Field menuField = Toolbar.class.getDeclaredField("mMenuBuilderCallback");
    menuField.setAccessible(true);
    final Field presenterField = Toolbar.class.getDeclaredField("mActionMenuPresenterCallback");
    presenterField.setAccessible(true);
    final Field menuViewField = Toolbar.class.getDeclaredField("mMenuView");
    menuViewField.setAccessible(true);

    final MenuPresenter.Callback currentPresenterCb = (MenuPresenter.Callback) presenterField
        .get(toolbar);
    if (!(currentPresenterCb instanceof ATHMenuPresenterCallback)) {
      final ATHMenuPresenterCallback newPresenterCb = new ATHMenuPresenterCallback(context,
          menuWidgetColor, currentPresenterCb, toolbar);
      final MenuBuilder.Callback currentMenuCb = (MenuBuilder.Callback) menuField.get(toolbar);
      toolbar.setMenuCallbacks(newPresenterCb, currentMenuCb);
      ActionMenuView menuView = (ActionMenuView) menuViewField.get(toolbar);
      if (menuView != null) {
        menuView.setMenuCallbacks(newPresenterCb, currentMenuCb);
      }
    }

    // OnMenuItemClickListener to tint submenu items
    final Field menuItemClickListener = Toolbar.class
        .getDeclaredField("mOnMenuItemClickListener");
    menuItemClickListener.setAccessible(true);
    Toolbar.OnMenuItemClickListener currentClickListener = (Toolbar.OnMenuItemClickListener) menuItemClickListener
        .get(toolbar);
    if (!(currentClickListener instanceof ATHOnMenuItemClickListener)) {
      final ATHOnMenuItemClickListener newClickListener = new ATHOnMenuItemClickListener(context,
          menuWidgetColor, currentClickListener, toolbar);
      toolbar.setOnMenuItemClickListener(newClickListener);
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example 3
Source File: DetailFragment.java    From fingerpoetry-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void initToolbarMenu(Toolbar toolbar) {
    toolbar.inflateMenu(R.menu.menu_article_pop);
    menu = toolbar.getMenu();
}
 
Example 4
Source File: DetailFragment.java    From Advanced_Android_Development with Apache License 2.0 4 votes vote down vote up
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if (data != null && data.moveToFirst()) {
        ViewParent vp = getView().getParent();
        if ( vp instanceof CardView ) {
            ((View)vp).setVisibility(View.VISIBLE);
        }

        // Read weather condition ID from cursor
        int weatherId = data.getInt(COL_WEATHER_CONDITION_ID);

        if ( Utility.usingLocalGraphics(getActivity()) ) {
            mIconView.setImageResource(Utility.getArtResourceForWeatherCondition(weatherId));
        } else {
            // Use weather art image
            Glide.with(this)
                    .load(Utility.getArtUrlForWeatherCondition(getActivity(), weatherId))
                    .error(Utility.getArtResourceForWeatherCondition(weatherId))
                    .crossFade()
                    .into(mIconView);
        }

        // Read date from cursor and update views for day of week and date
        long date = data.getLong(COL_WEATHER_DATE);
        String dateText = Utility.getFullFriendlyDayString(getActivity(),date);
        mDateView.setText(dateText);

        // Get description from weather condition ID
        String description = Utility.getStringForWeatherCondition(getActivity(), weatherId);
        mDescriptionView.setText(description);
        mDescriptionView.setContentDescription(getString(R.string.a11y_forecast, description));

        // For accessibility, add a content description to the icon field. Because the ImageView
        // is independently focusable, it's better to have a description of the image. Using
        // null is appropriate when the image is purely decorative or when the image already
        // has text describing it in the same UI component.
        mIconView.setContentDescription(getString(R.string.a11y_forecast_icon, description));

        // Read high temperature from cursor and update view
        boolean isMetric = Utility.isMetric(getActivity());

        double high = data.getDouble(COL_WEATHER_MAX_TEMP);
        String highString = Utility.formatTemperature(getActivity(), high);
        mHighTempView.setText(highString);
        mHighTempView.setContentDescription(getString(R.string.a11y_high_temp, highString));

        // Read low temperature from cursor and update view
        double low = data.getDouble(COL_WEATHER_MIN_TEMP);
        String lowString = Utility.formatTemperature(getActivity(), low);
        mLowTempView.setText(lowString);
        mLowTempView.setContentDescription(getString(R.string.a11y_low_temp, lowString));

        // Read humidity from cursor and update view
        float humidity = data.getFloat(COL_WEATHER_HUMIDITY);
        mHumidityView.setText(getActivity().getString(R.string.format_humidity, humidity));
        mHumidityView.setContentDescription(getString(R.string.a11y_humidity, mHumidityView.getText()));
        mHumidityLabelView.setContentDescription(mHumidityView.getContentDescription());

        // Read wind speed and direction from cursor and update view
        float windSpeedStr = data.getFloat(COL_WEATHER_WIND_SPEED);
        float windDirStr = data.getFloat(COL_WEATHER_DEGREES);
        mWindView.setText(Utility.getFormattedWind(getActivity(), windSpeedStr, windDirStr));
        mWindView.setContentDescription(getString(R.string.a11y_wind, mWindView.getText()));
        mWindLabelView.setContentDescription(mWindView.getContentDescription());

        // Read pressure from cursor and update view
        float pressure = data.getFloat(COL_WEATHER_PRESSURE);
        mPressureView.setText(getString(R.string.format_pressure, pressure));
        mPressureView.setContentDescription(getString(R.string.a11y_pressure, mPressureView.getText()));
        mPressureLabelView.setContentDescription(mPressureView.getContentDescription());

        // We still need this for the share intent
        mForecast = String.format("%s - %s - %s/%s", dateText, description, high, low);

    }
    AppCompatActivity activity = (AppCompatActivity)getActivity();
    Toolbar toolbarView = (Toolbar) getView().findViewById(R.id.toolbar);

    // We need to start the enter transition after the data has loaded
    if ( mTransitionAnimation ) {
        activity.supportStartPostponedEnterTransition();

        if ( null != toolbarView ) {
            activity.setSupportActionBar(toolbarView);

            activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    } else {
        if ( null != toolbarView ) {
            Menu menu = toolbarView.getMenu();
            if ( null != menu ) menu.clear();
            toolbarView.inflateMenu(R.menu.detailfragment);
            finishCreatingMenu(toolbarView.getMenu());
        }
    }
}