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. Get the height of the statusBar of Android
Forum Updated to NodeBB v4.3 + New Features

Get the height of the statusBar of Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 2 Posters 2.5k 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.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by
    #1

    From stackOverflow, the solution is

    Rect rectangle = new Rect();
    Window window = getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
    int statusBarHeight = rectangle.top;
    int contentViewTop = 
    window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int titleBarHeight= contentViewTop - statusBarHeight;
    

    Problem is, how could I obtain the Activity Qt is using?

    package com.tham;
    
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    
    import android.app.Activity;
    import android.graphics.Rect;
    import android.view.Window;
    
    public class AndroidUtils
    {    
        public static int statusBarHeight()
        {
            Rect rectangle = new Rect();
            //This line do not work since AndroidUtils do not inherit from Activity
            Window window = getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
            int statusBarHeight = rectangle.top;
            int contentViewTop =
            window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            return contentViewTop - statusBarHeight;
        }
    }
    

    Should I

    1. Call the c++ api QtAndroid::androidActivity() from java(KDAB introduce the solutoin, haven' t tried yet)
    2. Pass the QAndroidJniObject in to the java api--
      public static int statusBarHeight(Activity myActivityReference)

    If 2 is better, how should I pass the Activity into the jni properly? Thanks

    1 Reply Last reply
    0
    • thamT Offline
      thamT Offline
      tham
      wrote on last edited by tham
      #2

      I found a way to pass the Activity from c++ to jni, although I am not sure this is a correct solution or not

      int get_native_status::statusBarHeight() const
      {
          QAndroidJniObject activity = QtAndroid::androidActivity();
          if(activity.isValid()){
              return QAndroidJniObject::callStaticMethod<jint>
                                  ("com/tham/AndroidUtils" // class name
                                  , "statusBarHeight" // method name
                                  , "(Ljava/lang/Object;)I",
                                  activity.object<jobject>()
                                  );
          }else{
              qDebug()<<"activity is invalid";
          }    
      
          return 0;
      }
      

      AndroidUtils.java

      package com.tham;
      
      import android.content.res.Resources;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      
      import android.app.Activity;
      import android.graphics.Rect;
      import android.view.Window;
      
      public class AndroidUtils
      {    
          public static int statusBarHeight(Object myActivityReference)
          {
              Rect rectangle = new Rect();		
              Window window = ((Activity)myActivityReference).getWindow();
              window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
              int statusBarHeight = rectangle.top;
              int contentViewTop =
              window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
              return contentViewTop - statusBarHeight;
          }
      }
      

      Problem is this api give me weird value : -84.
      Do anyone know how to solve it?Thanks

      Edit : looks like -84 is ok, I just need to change multiply it with "-"

      J.HilkJ 1 Reply Last reply
      0
      • thamT tham

        I found a way to pass the Activity from c++ to jni, although I am not sure this is a correct solution or not

        int get_native_status::statusBarHeight() const
        {
            QAndroidJniObject activity = QtAndroid::androidActivity();
            if(activity.isValid()){
                return QAndroidJniObject::callStaticMethod<jint>
                                    ("com/tham/AndroidUtils" // class name
                                    , "statusBarHeight" // method name
                                    , "(Ljava/lang/Object;)I",
                                    activity.object<jobject>()
                                    );
            }else{
                qDebug()<<"activity is invalid";
            }    
        
            return 0;
        }
        

        AndroidUtils.java

        package com.tham;
        
        import android.content.res.Resources;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        
        import android.app.Activity;
        import android.graphics.Rect;
        import android.view.Window;
        
        public class AndroidUtils
        {    
            public static int statusBarHeight(Object myActivityReference)
            {
                Rect rectangle = new Rect();		
                Window window = ((Activity)myActivityReference).getWindow();
                window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
                int statusBarHeight = rectangle.top;
                int contentViewTop =
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
                return contentViewTop - statusBarHeight;
            }
        }
        

        Problem is this api give me weird value : -84.
        Do anyone know how to solve it?Thanks

        Edit : looks like -84 is ok, I just need to change multiply it with "-"

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #3

        hi @tham ,

        I'm interested in this topic as well, could you share some more code ?

        do you use QWidgets or QML as your main view port?
        how did set up the full screen on qml/cpp side?
        do you paint below the statusbar a custom background?
        How does it behave, when you force a notch/cutout from the developer settings

        PS:
        as requested, I moved the topic to Mobile and Embedded.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        thamT 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          hi @tham ,

          I'm interested in this topic as well, could you share some more code ?

          do you use QWidgets or QML as your main view port?
          how did set up the full screen on qml/cpp side?
          do you paint below the statusbar a custom background?
          How does it behave, when you force a notch/cutout from the developer settings

          PS:
          as requested, I moved the topic to Mobile and Embedded.

          thamT Offline
          thamT Offline
          tham
          wrote on last edited by
          #4

          @j-hilk said in Get the height of the statusBar of Android:

          hi @tham ,
          I'm interested in this topic as well, could you share some more code ?

          Hi, this is a commercial project, so I can't share it, sorry about that.

          do you use QWidgets or QML as your main view port?

          QML, using components from Felgo too

          do you paint below the statusbar a custom background?

          Yes, it it just a rectangle with the same color as the icon

          How does it behave, when you force a notch/cutout from the developer settings

          Not sure what is this mean, if you mean the statusBarHeight function, it works very well.

          as requested, I moved the topic to Mobile and Embedded.

          Thanks :)

          J.HilkJ 1 Reply Last reply
          0
          • thamT tham

            @j-hilk said in Get the height of the statusBar of Android:

            hi @tham ,
            I'm interested in this topic as well, could you share some more code ?

            Hi, this is a commercial project, so I can't share it, sorry about that.

            do you use QWidgets or QML as your main view port?

            QML, using components from Felgo too

            do you paint below the statusbar a custom background?

            Yes, it it just a rectangle with the same color as the icon

            How does it behave, when you force a notch/cutout from the developer settings

            Not sure what is this mean, if you mean the statusBarHeight function, it works very well.

            as requested, I moved the topic to Mobile and Embedded.

            Thanks :)

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @tham said in Get the height of the statusBar of Android:

            Hi, this is a commercial project, so I can't share it, sorry about that.

            Awhe, to bad 😥

            QML, using components from Felgo too

            Great, my Projects are usually also QML based. Never used Felgo and don't plan on using it 😋

            do you paint below the statusbar a custom background?

            Yes, it it just a rectangle with the same color as the icon

            So may I ask, how did you make your app full screen, and make it, that it's painted below the status bar.
            All I manage is either a black background behind the Statusbar(app doesn't reach that far up), or the App is painted over the Statusbar all together.

            How does it behave, when you force a notch/cutout from the developer settings

            Not sure what is this mean, if you mean the statusBarHeight function, it works very well.

            I actually meant this setting:
            https://developer.android.com/guide/topics/display-cutout
            You can simulate one in the developer settings of any android device.


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0

            • Login

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