Android Question [SOLVED] gradientdrawable android 10 b4a

manuel_g

Member
Licensed User
Hello. Sorry for my english.

I'm using the next code for "roundering the corner" of a Label and it works fine but in Android 10 does not work, the corner is flat

B4X:
Dim l_din_redondeocolor(2) As Int
l_din_redondeocolor(0)=Colors.Transparent
l_din_redondeocolor(1)=Colors.Transparent

Dim din_redondeo As GradientDrawable
din_redondeo.Initialize("TL_BR",l_din_redondeocolor)
din_redondeo.CornerRadius = 100dip

MyLabel.Background=din_redondeo

The Manifest is set as "<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="30"/>"
Should I change something in the Manifest?

Thank you all!
 

manuel_g

Member
Licensed User
What are the width and height dimensions of the Label?
Maybe 100dip for the corner radius is too big.

Hi Klaus.
No, on my devices with Android<10 works pretty good, only on devices Android 10 does not work, the label appears as if CornerRadius = 0dip

Thank you for replying me :)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly doesn't work.
You are setting the colors to transparent therefore you will not see any effect.
Even if the corner radius is too big it works, didn't remember this.

I tested it with Android 11 and it works, I don't have a device with Android 10

Attached my test project.

As both colors are the same you could also get the same effect it with a ColorDrawable.
 

Attachments

  • LabelGradienRadius.zip
    9.3 KB · Views: 178
Upvote 0

manuel_g

Member
Licensed User
Klaus, thanks for your time!!!!

I was using GradientRadius instead of ColorDrawable and I did not really need it. I changed the code and it works perfect on any Android version

B4X:
Dim din_redondeo As ColorDrawable
din_redondeo.Initialize(Colors.black,100dip)
MyLabel.Background=din_redondeo

Nevertheless, I show you the bizarre case with the Old code and its behavior on Android 10 and Android any. Its kind of rare than it works different dependding of the Android version.

Android ANY ---------- Android 10

GradientRadius.png


Again, THANK YOU SO MUCH!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Looks strange.
Which image is Android 10 ?
In the left image, it seems to me that the Top position of the Label is out of the screen, because, when you define a corner radius, it is the same for all corners and the two upper ones don't have a radius.
In your example you set the corner radius to 100dip.
I am sure that the radius in your left image is less than 100dip.
For the right image I do not understand what happened.
Could you post a small project showing the problem?
 
Upvote 0

manuel_g

Member
Licensed User
Hi,
In the left image, it seems to me that the Top position of the Label is out of the screen, because, when you define a corner radius, it is the same for all corners and the two upper ones don't have a radius.
Yes, the top position is out of the screen.

I don't know why the project.zip is around 0.7Mb, so I coudln't upload it. I attached a .txt, please just copy/paste it in a new Module, you won't need any layout

These are the results:

For NOT Android 10:
Not Android 10.jpeg


For Android 10:
Android 10.jpeg


The red label is created with GradientDrawable
The black label is created with ColorDrawable

It seems like abstract art... jaja
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem is this:

B4X:
    Dim l_din_redondeocolor(2) As Int
     l_din_redondeocolor(0)=Colors.Transparent
     l_din_redondeocolor(1)=Colors.Transparent
  
    Dim din_redondeo As GradientDrawable
    din_redondeo.Initialize("TL_BR",l_din_redondeocolor)
    din_redondeo.CornerRadius = 100dip
  
    l1.Initialize("l")
    Activity.AddView(l1,Activity.Width/8,Activity.Height*7/20,Activity.Width*3/4,Activity.Height/20)
    l1.Background=din_redondeo
    l1.Color=Colors.Red
You are setting the background to transparent gradient, which is strange because with transparent you will see nothing.
I was already astonished in post #4.
BUT, then you reset the background color with l1.Color=Colors.Red, that is the problem.
Setting the background with the Color property sets the corner radii to ZERO.

The code below works also with GradientDrawable.
B4X:
    Dim l_din_redondeocolor(2) As Int
    l_din_redondeocolor(0)=Colors.Red 
    l_din_redondeocolor(1)=Colors.Red

    Dim din_redondeo As GradientDrawable
    din_redondeo.Initialize("TL_BR",l_din_redondeocolor)

    din_redondeo.CornerRadius = 100dip
    l1.Initialize("l"
    Activity.AddView(l1,Activity.Width/8,Activity.Height*7/20,Activity.Width*3/4,Activity.Height/20)
    l1.Background=din_redondeo

PS: This shows once more that without all the information it is often difficult or impossible to give concrete help.
Without having seen the full code, no helper would have thought that the background was reset somewhere else, because, in the code snippet, it was missing.
This is the reason why I almost always ask for a project showing the problem.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
To make a Label like in post #5, no need to move the Label partly out of the screen you can set different corner radii with a JavaObject.
And you can play with it.

1617534649045.png


Attached the demo project.

Code for the top Label:

B4X:
    Private cols(2) As Int
    cols(0)=Colors.RGB(72, 61, 139)
    cols(1)=Colors.Blue
   
    Private gdw As GradientDrawable
    gdw.Initialize("TOP_BOTTOM", cols)
    Private jo As JavaObject
    jo = gdw
    jo.RunMethod("setCornerRadii", Array As Object(Array As Float(0, 0, 0, 0, 30dip, 30dip, 30dip, 30dip)))
    lblTop.Background = gdw

Documentation:
https://developer.android.com/refer...able/GradientDrawable#setCornerRadii(float[])
 

Attachments

  • LabelCornerRadii.zip
    9.9 KB · Views: 179
Upvote 0

manuel_g

Member
Licensed User
Hi!

PS: This shows once more that without all the information it is often difficult or impossible to give concrete help.

You're totally right! I will always attach a small project since the first post. I swear

To make a Label like in post #5, no need to move the Label partly out of the screen you can set different corner radii with a JavaObject.

Thank you for this! I'll upgrade my code.


BUT, then you reset the background color with l1.Color=Colors.Red, that is the problem.
I understand what you said and my mistake, but... Why it works different deppending of the Android version? I mean, if I had not would test it on an Android 10 I had not never realized I was making a mistake because on other devices works "fine".

THANKS A LOT for your time.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Why it works different depending of the Android version?
I don't know ! I am afraid that this is another Google mystery when they change versions.
I tested your code on my tablet running Android 7 and I confirm that the behavior is different.
 
Upvote 0
Top