code: private void obtainTitleColor(final int theme) {
          TypedArray typedArray = getContext().getTheme()
                  .obtainStyledAttributes(theme, new int[]{android.R.attr.textColorPrimary});
          int textColorPrimary = typedArray.getResourceId(0, 0);
          typedArray.recycle();
  
          if (textColorPrimary != 0) {
              int titleColor = ContextCompat.getColor(getContext(), textColorPrimary);
              toolbar.setTitleTextColor(titleColor);
          }
      }
docs: Obtains the color of the toolbar's title from a specific typed array.
  
  @param theme
  The resource id of the theme, which should be applied on the toolbar, as an {@link
  Integer} value