code: private void obtainBackgroundColor(final int theme) {
          TypedArray typedArray = getContext().getTheme()
                  .obtainStyledAttributes(theme, new int[]{R.attr.colorPrimary});
          int colorPrimary = typedArray.getColor(0, 0);
          typedArray.recycle();
  
          if (colorPrimary != 0) {
              backgroundView.setBackgroundColor(colorPrimary);
          }
      }
docs: Obtains the color of the toolbar's background 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