Get rounded corners on android
-
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? -
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.
@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
-
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. -
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.@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. -
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 fromgetDisplay().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. -
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 fromgetDisplay().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.@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. -
getRoundedCorner
Added in API level 31public 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) -
getRoundedCorner
Added in API level 31public 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)@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. -
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.