Class ChatUIThemeManager

  • All Implemented Interfaces:

    
    public class ChatUIThemeManager
    
                        

    Theme manager for the ChatUI SDK.

    Manages theme switching and resource resolution for the ChatUI components, supporting multiple themes and automatic light/dark mode adaptation.

    • Built-in theme: provides the lively theme (LIVELY_THEME), which automatically follows the system light/dark mode
    • Custom themes: developers can register and extend custom themes with light/dark variants and theme stacking
    • System follow: automatically switches with the system light/dark mode
    • Resource resolution: resolves theme attribute IDs and color values based on the current theme and dark-mode state
    • Auto-apply: applies themes to all Activities via
    • Theme listeners: notifies registered OnThemeListeners on every switch

    On some devices, toggling the system dark mode while the app is in the background may not take effect immediately because the system still reports the old value until the app restarts.

    Recommended solution: If your app uses the AppCompat library, call AppCompatDelegate.setDefaultNightMode(nightMode) in Application.onCreate() to ensure timely mode switching:

    // Follow system dark mode
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    
    // Force light mode
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
    // Force dark mode
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
    // 1. Switch to the lively theme (auto light/dark)
    ChatUIThemeManager.changeInnerTheme(context, ChatUIThemeManager.LIVELY_THEME);
    
    // 2. Register a custom theme with light/dark variants
    ChatUIThemeManager.addTheme("CUSTOM_THEME",
        R.style.MyCustomLightTheme,
        R.style.MyCustomDarkTheme
    );
    
    // 3. Switch to the custom theme, stacked on top of the lively theme
    ChatUIThemeManager.changeCustomTheme(context, "CUSTOM_THEME", ChatUIThemeManager.LIVELY_THEME);
    
    // 4. Resolve a theme attribute to a resource ID at runtime
    int bgResId = ChatUIThemeManager.getAttrResId(context, R.attr.nc_conversation_bg);
    view.setBackgroundResource(bgResId);
    
    // 5. Query the current theme
    String currentTheme = ChatUIThemeManager.getCurrentThemeName();
    
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public interface ChatUIThemeManager.OnThemeListener

      Listener for theme-change events.

      Registered via addThemeListener; invoked whenever the active theme changes.

      OnThemeListener listener = new OnThemeListener() {
          @Override
          public void onThemeChanged(Context context, String oldTheme, String newTheme) {
              if (context instanceof Activity) {
                  ((Activity) context).recreate();
              }
          }
      };
      
      ChatUIThemeManager.addThemeListener(listener);
      
      // Remove when no longer needed to avoid memory leaks
      ChatUIThemeManager.removeThemeListener(listener);
      
    • Field Summary

      Fields 
      Modifier and Type Field Description
      public final static String LIVELY_THEME
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static void addTheme(String themeType, int lightStyleResId, int darkStyleResId) Registers a theme with light and dark style variants.
      static void changeInnerTheme(Context context, String themeType) Switches to a registered built-in theme and applies it immediately.
      static void changeCustomTheme(Context context, String customThemeType, String baseOnTheme) Switches to a custom theme that is stacked on top of a base built-in theme.
      static String getCurrentThemeName() Returns the currently active theme identifier.
      static void addThemeListener(ChatUIThemeManager.OnThemeListener listener) Registers a theme-change listener.
      static void removeThemeListener(ChatUIThemeManager.OnThemeListener listener) Unregisters a previously registered theme-change listener.
      static int getAttrResId(Context context, int attrId) Resolves a theme attribute to its actual resource ID.
      static int getColorFromAttrId(Context context, int attrId) Resolves a theme color attribute to an ARGB color integer.
      static boolean isSystemInDarkMode(Context context) Returns whether the system is currently in dark mode.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • addTheme

         static void addTheme(String themeType, int lightStyleResId, int darkStyleResId)

        Registers a theme with light and dark style variants.

        Calling this method multiple times for the same themeType stacks the styles: each additional call appends a new layer, and later layers override earlier ones for the same attribute.

        // Register a custom theme with light/dark variants
        ChatUIThemeManager.addTheme(
            "CUSTOM_THEME",
            R.style.MyCustomLightTheme,
            R.style.MyCustomDarkTheme
        );
        
        // Stack additional overrides on top of an existing theme
        ChatUIThemeManager.addTheme(
            ChatUIThemeManager.LIVELY_THEME,
            R.style.MyOverrideLight,
            R.style.MyOverrideDark
        );
        
        Parameters:
        themeType - theme identifier (use UPPER_SNAKE_CASE, e.g.
        lightStyleResId - style resource ID for light mode (must be non-zero)
        darkStyleResId - style resource ID for dark mode (must be non-zero)
      • changeInnerTheme

         static void changeInnerTheme(Context context, String themeType)

        Switches to a registered built-in theme and applies it immediately.

        This method:

        • Validates the theme is registered
        • Selects the light or dark style based on the system dark-mode state
        • Applies the theme to the Application context (globally)
        • Applies the theme to the given context (immediately)
        • Automatically applies the theme to all future Activities via lifecycle callbacks

        If the target theme is already active, this method is a no-op.

        ChatUIThemeManager.changeInnerTheme(context, ChatUIThemeManager.LIVELY_THEME);
        
        Parameters:
        context - context (Activity or Application context recommended)
        themeType - theme identifier (LIVELY_THEME or any registered custom theme)
      • changeCustomTheme

         static void changeCustomTheme(Context context, String customThemeType, String baseOnTheme)

        Switches to a custom theme that is stacked on top of a base built-in theme.

        Application order:

        • All style layers from baseOnTheme are applied first
        • All style layers from customThemeType are applied on top
        • Later layers override earlier ones for conflicting attributes
        ChatUIThemeManager.addTheme("MY_BLUE_THEME",
            R.style.MyBlueLightTheme, R.style.MyBlueDarkTheme);
        
        ChatUIThemeManager.changeCustomTheme(context, "MY_BLUE_THEME",
            ChatUIThemeManager.LIVELY_THEME);
        
        Parameters:
        context - context (Activity or Application context recommended)
        customThemeType - custom theme identifier (must be registered via addTheme)
        baseOnTheme - base theme identifier to stack under the custom theme
      • getCurrentThemeName

         static String getCurrentThemeName()

        Returns the currently active theme identifier.

        String theme = ChatUIThemeManager.getCurrentThemeName();
        if (ChatUIThemeManager.LIVELY_THEME.equals(theme)) {
            // Currently using the lively theme
        }
        
        Returns:

        current theme identifier (e.g. LIVELY_THEME or a custom theme name)

      • addThemeListener

         static void addThemeListener(ChatUIThemeManager.OnThemeListener listener)

        Registers a theme-change listener.

        The listener is invoked on every theme switch. Duplicate registrations are silently ignored.

        Parameters:
        listener - the listener to register (must not be null)
      • getAttrResId

         static int getAttrResId(Context context, int attrId)

        Resolves a theme attribute to its actual resource ID.

        Parameters:
        context - context (must not be null)
        attrId - theme attribute ID (e.g.
        Returns:

        the resolved resource ID, or 0 if resolution fails

      • getColorFromAttrId

         static int getColorFromAttrId(Context context, int attrId)

        Resolves a theme color attribute to an ARGB color integer.

        Parameters:
        context - context (must not be null)
        attrId - color attribute ID (e.g.
        Returns:

        resolved ARGB color, or 0 if resolution fails

      • isSystemInDarkMode

         static boolean isSystemInDarkMode(Context context)

        Returns whether the system is currently in dark mode.