Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. anyone got an Android code to detect OS-theme (dark/light?) ?
Forum Updated to NodeBB v4.3 + New Features

anyone got an Android code to detect OS-theme (dark/light?) ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
12 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JoeCFDJ JoeCFD

    @TomZ Try this out.

        public static boolean isDarkTheme(Activity activity) {
            int currentNightMode = activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
            return currentNightMode == Configuration.UI_MODE_NIGHT_YES;
        }
    

    or

    package com.yourpackage;
    
    import android.content.Context;
    import android.content.res.Configuration;
    
    public class ThemeDetector {
        public static String getSystemTheme(Context context) {
            int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
            if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
                return "dark";
            } else {
                return "light";
            }
        }
    }
    
    TomZT Offline
    TomZT Offline
    TomZ
    wrote on last edited by TomZ
    #3

    @JoeCFD thank you!

    I've been trying for a couple of hours anything I can to actually call this from C++, this is as far as I have come (the docs on the Android stuff is significantly lower quality than the rest of Qt).

    But I can't figure it out. This doesn't compile. No clue what "Args" are as mentioned in the documentation.

    auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
    QJniObject retStr = QJniObject::callStaticObjectMethod("org/flowee/ThemeDetector",
    "getSystemTheme", "()Lorg/flowee/ThemeDetector;", context);
    

    yap, 2 lines. Still trying after 3 hours. [hair pulling emoti]

    ps. using Qt 6.5.1

    JoeCFDJ TomZT 2 Replies Last reply
    0
    • TomZT TomZ

      @JoeCFD thank you!

      I've been trying for a couple of hours anything I can to actually call this from C++, this is as far as I have come (the docs on the Android stuff is significantly lower quality than the rest of Qt).

      But I can't figure it out. This doesn't compile. No clue what "Args" are as mentioned in the documentation.

      auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
      QJniObject retStr = QJniObject::callStaticObjectMethod("org/flowee/ThemeDetector",
      "getSystemTheme", "()Lorg/flowee/ThemeDetector;", context);
      

      yap, 2 lines. Still trying after 3 hours. [hair pulling emoti]

      ps. using Qt 6.5.1

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #4

      @TomZ said in anyone got an Android code to detect OS-theme (dark/light?) ?:

      "()Lorg/flowee/ThemeDetector;"

      I guess this one is wrong. Here you define the data type you pass into the func

      "()Lorg/flowee/ThemeDetector;"
      

      for example passing a string is like

      "(Landorid/content/Context;)LJava/lang/String;"
      

      read the doc carefully.

      TomZT 2 Replies Last reply
      1
      • TomZT TomZ

        @JoeCFD thank you!

        I've been trying for a couple of hours anything I can to actually call this from C++, this is as far as I have come (the docs on the Android stuff is significantly lower quality than the rest of Qt).

        But I can't figure it out. This doesn't compile. No clue what "Args" are as mentioned in the documentation.

        auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
        QJniObject retStr = QJniObject::callStaticObjectMethod("org/flowee/ThemeDetector",
        "getSystemTheme", "()Lorg/flowee/ThemeDetector;", context);
        

        yap, 2 lines. Still trying after 3 hours. [hair pulling emoti]

        ps. using Qt 6.5.1

        TomZT Offline
        TomZT Offline
        TomZ
        wrote on last edited by
        #5

        another attempt in a different direction, a little progress made, I guess...

        auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
        auto resources = context.callObjectMethod("getResources", "()Landroid.content.res.Resources;");
        

        Got stuck on using 'callMethod' which didn't work, but callObjectMethod' does. No clue what the difference is.

        JoeCFDJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @TomZ said in anyone got an Android code to detect OS-theme (dark/light?) ?:

          "()Lorg/flowee/ThemeDetector;"

          I guess this one is wrong. Here you define the data type you pass into the func

          "()Lorg/flowee/ThemeDetector;"
          

          for example passing a string is like

          "(Landorid/content/Context;)LJava/lang/String;"
          

          read the doc carefully.

          TomZT Offline
          TomZT Offline
          TomZ
          wrote on last edited by
          #6

          @JoeCFD said in anyone got an Android code to detect OS-theme (dark/light?) ?:

          read the doc carefully.

          The only docs I found is the QJNiObject API docs. Do you know of any better ones?

          1 Reply Last reply
          0
          • TomZT TomZ

            another attempt in a different direction, a little progress made, I guess...

            auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
            auto resources = context.callObjectMethod("getResources", "()Landroid.content.res.Resources;");
            

            Got stuck on using 'callMethod' which didn't work, but callObjectMethod' does. No clue what the difference is.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #7

            @TomZ In Qt5, the following is good enough. Have not tried Qt6

            QAndroidJniObject::callStaticObjectMethod(...)
            

            This does not exist in Qt6 anymore.

            https://forum.qt.io/topic/138952/help-qt6-calling-android-java-from-c/2

            A full and pretty new example for Qt6:
            https://scythe-studio.com/de/blog/how-to-interface-qt-with-android-java-code-2023

            TomZT 1 Reply Last reply
            1
            • JoeCFDJ JoeCFD

              @TomZ In Qt5, the following is good enough. Have not tried Qt6

              QAndroidJniObject::callStaticObjectMethod(...)
              

              This does not exist in Qt6 anymore.

              https://forum.qt.io/topic/138952/help-qt6-calling-android-java-from-c/2

              A full and pretty new example for Qt6:
              https://scythe-studio.com/de/blog/how-to-interface-qt-with-android-java-code-2023

              TomZT Offline
              TomZT Offline
              TomZ
              wrote on last edited by
              #8

              @JoeCFD In Qt6 the calling of the class you pasted seems completely impossible by design.

              You can't pass in non-trivial objects, is what clang tells me.

              error: cannot pass object of non-trivial type 'QJniObject' through variadic function; call will abort at runtime [-Wnon-pod-varargs]
              "getSystemTheme", "(Landoroid/content/Context;)L;", context);
              
              JoeCFDJ 1 Reply Last reply
              0
              • TomZT TomZ

                @JoeCFD In Qt6 the calling of the class you pasted seems completely impossible by design.

                You can't pass in non-trivial objects, is what clang tells me.

                error: cannot pass object of non-trivial type 'QJniObject' through variadic function; call will abort at runtime [-Wnon-pod-varargs]
                "getSystemTheme", "(Landoroid/content/Context;)L;", context);
                
                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #9

                @TomZ you read my post above.

                1 Reply Last reply
                0
                • JoeCFDJ JoeCFD

                  @TomZ said in anyone got an Android code to detect OS-theme (dark/light?) ?:

                  "()Lorg/flowee/ThemeDetector;"

                  I guess this one is wrong. Here you define the data type you pass into the func

                  "()Lorg/flowee/ThemeDetector;"
                  

                  for example passing a string is like

                  "(Landorid/content/Context;)LJava/lang/String;"
                  

                  read the doc carefully.

                  TomZT Offline
                  TomZT Offline
                  TomZ
                  wrote on last edited by
                  #10

                  @JoeCFD said in anyone got an Android code to detect OS-theme (dark/light?) ?:

                  read the doc carefully.

                  maybe that changed between Qt5 and Qt6, but in the QJniObject docs it states:

                  Method signatures are written as "(ArgumentsTypes)ReturnType", see JNI Types.

                  JoeCFDJ 1 Reply Last reply
                  0
                  • TomZT TomZ

                    @JoeCFD said in anyone got an Android code to detect OS-theme (dark/light?) ?:

                    read the doc carefully.

                    maybe that changed between Qt5 and Qt6, but in the QJniObject docs it states:

                    Method signatures are written as "(ArgumentsTypes)ReturnType", see JNI Types.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #11

                    @TomZ I posted two links above. The second one has detailed info.

                    TomZT 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @TomZ I posted two links above. The second one has detailed info.

                      TomZT Offline
                      TomZT Offline
                      TomZ
                      wrote on last edited by
                      #12

                      @JoeCFD sweet!

                      in the mean time I figured out the pure JNI code;

                      auto context = QJniObject(QNativeInterface::QAndroidApplication::context());
                      // In Java:
                      //   context.getResources().getConfiguration().uiMode
                      auto resources = context.callObjectMethod("getResources",
                      "()Landroid/content/res/Resources;");
                      auto config = resources.callObjectMethod("getConfiguration",
                      "()Landroid/content/res/Configuration;");
                      auto uiMode = config.getField<jint>("uiMode");
                      constexpr int UI_MODE_NIGHT_MASK = 0x30;
                      constexpr int UI_MODE_NIGHT_YES = 0x20;
                      const bool dark = (uiMode & UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES;
                      
                      1 Reply Last reply
                      0
                      • TomZT TomZ has marked this topic as solved on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved