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 rounded corners on android
QtWS25 Last Chance

Get rounded corners on android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
13 Posts 2 Posters 142 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.
  • F Offline
    F Offline
    felsi
    wrote last edited by
    #1

    Hello!

    I am looking for a way to get the radius of the rounded corners of my phones display.
    Maybe someone knows, if there are any plans to implement this in the near future in a cross platform fashion?
    Like the expanded client area recently...

    At the moment, i try to find a solution for android.

    I extended the QtActivity (simplified):

    public class My_Activity extends QtActivity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            this.getCornerRadius();
            this.getSafeInsetBottom();
        }
        
        @Override
        public void onAttachedToWindow()
        {
            super.onAttachedToWindow();
            this.getCornerRadius();
        }
        
        @Override
        public void onConfigurationChanged(Configuration newConfig)
        {
            super.onConfigurationChanged(newConfig);
            this.getCornerRadius();
        }
    
        public void getCornerRadius()
        {
            getDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
            // getWindowManager().getDefaultDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
            // getWindow().getDecorView().getRootWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
            // getWindow().getWindowManager().getMaximumWindowMetrics().getWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
        }
        
        public int getSafeInsetTop()
        {
            int safe_inset_top = getDisplay().getCutout().getSafeInsetTop();
            Log.d("My_Activity_Debug", "safe_inset_top: " + safe_inset_top);
            return safe_inset_top;
        }
    }
    

    But none of my approaches work, getRoundedCorner() always returns null, like there where no rounded corners.
    I was, however, able to get the cutout inset.
    It seems, that noone else in the world had ever had this problem, so maybe it is Qt related...
    I do not know enough about how Qt for Android works to evaluate this.

    So maybe, someone could provide me with some relevant informations?
    Or has a completely other approach to solve my problem?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CassD
      wrote last edited by
      #2

      hi

      are you sure you are trying to make this at the appropriate place in your code and in the appropriate way ?

      I just figure out that making your top most item a Rectangle with the desired radius is the appropriate solution.

      F 1 Reply Last reply
      0
      • C CassD

        hi

        are you sure you are trying to make this at the appropriate place in your code and in the appropriate way ?

        I just figure out that making your top most item a Rectangle with the desired radius is the appropriate solution.

        F Offline
        F Offline
        felsi
        wrote last edited by felsi
        #3

        @CassD
        I read a few times, that this should be called in onAttachedToWindow(), because it only works, when the view has already been attached to the window. But it didn't solve it for me.
        I also tried to trigger getCornerRadius() via button at some random time.
        I even called it via QNativeInterface::QAndroidApplication::runOnAndroidMainThread().
        The java code gets definitely executed, i have a lot of debugs und null checks.

        Sorry, i don't understand...
        Yes, i need the radius to adjust the items in my window in QML.
        It is fullscreen.

        By the way:

        • Qt 6.9.0
        • build-tools-Version: 35.0.0
        • Build-platform-SDK: android-35
        • Android-Version on phone: 14
        • ndk 26.1.10909125
        • openjdk 17
        • debian 11
        1 Reply Last reply
        0
        • C Offline
          C Offline
          CassD
          wrote last edited by
          #4

          oh sorry, from your title I thought understanding you wanted to make rounded corner , while you actually want to get the value defined in the app.

          where did you get that documentation from ? I just guess that QtActivity inherits from Android's Activity, but I don't even find the getCornerRadius neither on Activity's documentation nor on it's base classes or interfaces.
          Googling "android getCornerRadius" or anything so doesn't return me any relevant result.

          F 1 Reply Last reply
          0
          • C CassD

            oh sorry, from your title I thought understanding you wanted to make rounded corner , while you actually want to get the value defined in the app.

            where did you get that documentation from ? I just guess that QtActivity inherits from Android's Activity, but I don't even find the getCornerRadius neither on Activity's documentation nor on it's base classes or interfaces.
            Googling "android getCornerRadius" or anything so doesn't return me any relevant result.

            F Offline
            F Offline
            felsi
            wrote last edited by
            #5

            @CassD
            No, look, i wrote getCornerRadius(), it's a member function of My_Activity.
            My problem is, that the following returns me a null instead of an instance of android.view.RoundedCorner.

            getWindow().getDecorView().getRootWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius()
            

            https://developer.android.com/reference/android/view/WindowInsets#getRoundedCorner(int)
            This is the most recommended approach, you'll find on the internet.
            But it doesn't work for me.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CassD
              wrote last edited by
              #6

              oh yes sorry, I didn't scroll down enough on your code.

                  public void getCornerRadius()
                  {
                      getDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                      // getWindowManager().getDefaultDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                      // getWindow().getDecorView().getRootWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                      // getWindow().getWindowManager().getMaximumWindowMetrics().getWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                  }
              

              so you have a getter method with a void "return type" ... So either it's a getter and should return something, and thus shouldn't be a void method, or it's not a getter and thus shouldn't start with the 'get' prefix.
              you get a value from getDisplay().getRoundedCorner(corner).getRadius(), but don't return it.
              how do you know that this line returns nothing ? did you save it's result to a variable and print it to console ?
              and same for the three next commented lines.
              There's no point at all in getting those values if you do nothing with them.

              F 1 Reply Last reply
              0
              • C CassD

                oh yes sorry, I didn't scroll down enough on your code.

                    public void getCornerRadius()
                    {
                        getDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                        // getWindowManager().getDefaultDisplay().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                        // getWindow().getDecorView().getRootWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                        // getWindow().getWindowManager().getMaximumWindowMetrics().getWindowInsets().getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT).getRadius();
                    }
                

                so you have a getter method with a void "return type" ... So either it's a getter and should return something, and thus shouldn't be a void method, or it's not a getter and thus shouldn't start with the 'get' prefix.
                you get a value from getDisplay().getRoundedCorner(corner).getRadius(), but don't return it.
                how do you know that this line returns nothing ? did you save it's result to a variable and print it to console ?
                and same for the three next commented lines.
                There's no point at all in getting those values if you do nothing with them.

                F Offline
                F Offline
                felsi
                wrote last edited by
                #7

                @CassD
                Thank you for your response.
                As i wrote in my first post, this is a simplified version of my code to point directly to my problem.
                As i wrote in my second post, i have a lot of debugs and null checks.
                The 4 different approaches would make the sample code hard to read...
                I don't have a problem with basics, i do other stuff in My_Activity, which all works fine.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  CassD
                  wrote last edited by CassD
                  #8

                  getRoundedCorner
                  Added in API level 31

                  public RoundedCorner getRoundedCorner (int position)

                  Returns the RoundedCorner of the given position if there is one.

                  Returns
                  RoundedCorner the rounded corner of the given position. Returns null if there is none.

                  so if there's none, it returns null, normal.
                  You're trying to get the value of an object that doesn't exist.
                  https://developer.android.com/reference/android/view/Display#getRoundedCorner(int)

                  F 1 Reply Last reply
                  0
                  • C CassD

                    getRoundedCorner
                    Added in API level 31

                    public RoundedCorner getRoundedCorner (int position)

                    Returns the RoundedCorner of the given position if there is one.

                    Returns
                    RoundedCorner the rounded corner of the given position. Returns null if there is none.

                    so if there's none, it returns null, normal.
                    You're trying to get the value of an object that doesn't exist.
                    https://developer.android.com/reference/android/view/Display#getRoundedCorner(int)

                    F Offline
                    F Offline
                    felsi
                    wrote last edited by
                    #9

                    @CassD
                    Yes, but i am pretty sure, that the corners are rounded, i can see them on my phone. ;)

                    package My_Package;
                    
                    import org.qtproject.qt.android.bindings.QtActivity;
                    
                    import android.util.Log;
                    
                    import android.os.Bundle;
                    
                    import android.os.Build.VERSION;
                    
                    import android.view.Window;
                    import android.view.Display;
                    import android.view.RoundedCorner;
                    import android.content.res.Configuration;
                    
                    public class My_Activity extends QtActivity
                    {
                        public int android_api = 0;
                    
                        @Override
                        public void onCreate(Bundle savedInstanceState)
                        {
                            super.onCreate(savedInstanceState);
                    
                            this.android_api = android.os.Build.VERSION.SDK_INT;
                            Log.d("My_Activity_Debug", "onCreate: android_api:" + this.android_api);
                    
                            this.getRadiusBottom();
                        }
                    
                        @Override
                        public void onAttachedToWindow()
                        {
                            Log.d("My_Activity_Debug", "onAttachedToWindow");
                            super.onAttachedToWindow();
                            this.getRadiusBottom();
                        }
                    
                        @Override
                        public void onConfigurationChanged(Configuration newConfig)
                        {
                            Log.d("My_Activity_Debug", "onConfigurationChanged");
                            super.onConfigurationChanged(newConfig);
                            this.getRadiusBottom();
                        }
                    
                        public int getRadiusBottom()
                        {
                            if(android_api>=31)
                            {
                                Display display = getDisplay();
                                if(display==null)
                                {
                                    Log.d("My_Activity_Debug", "getRadiusBottom: could not get display");
                                    return 0;
                                }
                    
                                RoundedCorner bottom_left_corner = display.getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT);
                                if(bottom_left_corner==null)
                                {
                                    Log.d("My_Activity_Debug", "getRadiusBottom: could not get rounded corner");
                                    return 0;
                                }
                    
                                int bottom_left_radius = bottom_left_corner.getRadius();
                                Log.d("My_Activity_Debug", "getRadiusBottom: bottom_left_radius: " + bottom_left_radius);
                                return bottom_left_radius;
                            }
                            else
                            {
                                return 0;
                            }
                        }
                    }
                    

                    Please, feel free to test it, just copy paste and use it in an empty project.
                    The other approaches follow the same pattern.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      CassD
                      wrote last edited by
                      #10

                      So if I understand right (at the beginning I clearly misunderstood it), you want to get the radii values of your phone's screen ? is that right ?
                      Yeah I never paid attention that my phone's screen has rounded corners so I didn't get it.

                      I feel like it's an android issue, not a Qt one, and wouldn't be surprized that it would be device specific.
                      did you try it directly in android studio in pure java / android project ? (without any Qt related code)
                      did you really try it on your phone (looks like yes according to your last reply) ? or just in android simulator ?
                      what about other devices ?

                      coz in fact now what I'm thinking about is : how did the manufacturer of your phone actually implement it ? There are so many phone manufacturers on android that it wouldn't be surprizing that a few (especially on the low range ones) cut a few corners and contented themselves just to move header and footer elements so that they don't interfere with corners without reimplementing that functionnality.

                      F 1 Reply Last reply
                      0
                      • C CassD

                        So if I understand right (at the beginning I clearly misunderstood it), you want to get the radii values of your phone's screen ? is that right ?
                        Yeah I never paid attention that my phone's screen has rounded corners so I didn't get it.

                        I feel like it's an android issue, not a Qt one, and wouldn't be surprized that it would be device specific.
                        did you try it directly in android studio in pure java / android project ? (without any Qt related code)
                        did you really try it on your phone (looks like yes according to your last reply) ? or just in android simulator ?
                        what about other devices ?

                        coz in fact now what I'm thinking about is : how did the manufacturer of your phone actually implement it ? There are so many phone manufacturers on android that it wouldn't be surprizing that a few (especially on the low range ones) cut a few corners and contented themselves just to move header and footer elements so that they don't interfere with corners without reimplementing that functionnality.

                        F Offline
                        F Offline
                        felsi
                        wrote last edited by
                        #11

                        @CassD
                        Exactly, and i need to deal with them, because my app is fullscreen.

                        Thank you for your advice, i set up android studio and tried the code with a sample project.
                        With the exact same result, so it's not Qt related.
                        Yes, i use a Samsung A15.

                        I am pretty surprised. Providing a few ints shouldn't be that hard...

                        Nevertheless, should i create a feature request?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          CassD
                          wrote last edited by
                          #12

                          hi

                          Google may provide some API to get some minor characteristics of the device, it doesn't mean that the manufacturer of the device implemented it. Some features like screen width or height are of course really important, I don't think it's the case of corner radius. this kind of feature is something you can duely expect to be implemented by google (or apple) on their own hardware if they provide API to retrieve those values, but it doesn't mean that other manufacturers of android phones will bother with. I just guess this is just one out of many ultra minor characteristics that all combined is time consuming to implement for a very minor usage. Still might also be an undeliberate miss from Samsung.

                          You still can try a feature request, but will it be responded ? I seriously doubt there's a point for Qt to waste any time or energy on a fancy that everyone deals without.
                          After all, many devs create android apps with Qt (and I just guess it's no different with other frameworks) without even ever caring about corner radius or even the cutout inset. So do you really need to care about ?
                          If I was a decision maker for a phone manufacturer, it's probably the kind of fancy I wouldn't bother with.

                          You still can try to determine a value that is high enough to accomodate most phones on the market, but not too high not to cause any unnecessary disruption for the user, and use that as a default value in case of null result.

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            felsi
                            wrote last edited by
                            #13

                            Are you sure? This seems pretty nonprofessional.
                            I have no better explanation, though.

                            I would assume, that this affects every fullscreen app...
                            I write a media player, for example.
                            They recently implemented this:
                            https://www.qt.io/blog/expanded-client-areas-and-safe-areas-in-qt-6.9
                            Which seems to me quite similar.

                            Yes, as a workaround, i use the height of the hidden status bar...
                            Producing wasted space on old phones without rounded corners.

                            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