Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get the height of the statusBar of Android



  • 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



  • 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 "-"


  • Moderators

    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.



  • @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 :)


  • Moderators

    @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.


Log in to reply