Android Question setCornerRadii, Again

Gavin

Member
Licensed User
Longtime User
Hi to everyone.
It has been a while since my last post.

I am having what appears to be an unusual problem with "setCornerRadii"

The following code is an extract from the Sudoku program I have been working on, (on and off) for years.
I use "setCornerRadii" to change the shape of cells which can be swapped.

What I have just discovered is that if you try to run "setCornerRadii" on Cell(80), the program will crash but, if you comment out line 118 in the following code, the program runs perfectly.
I have been trying to figure out what the devil is happening for two days, with no success and no idea.

Any help greatly appreciated.

I hope I have posted the code correctly or, should I have included the code as an attached .zip file.


[CODE lang="b4x" title="setCornerRadii Example" highlight="118"]#Region Project Attributes
#ApplicationLabel: Set Corner Radii
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type CellData (Security As String, RCC As String, Row As Int, Column As Int, Group As Int, LastEntry As String, _
Klicked As Int, HighLighted As Boolean, Following As String, Swapable As Boolean, SwapWith As String, _
SwapShape As String, ReadyToSwap As Boolean)
Dim CellInfo(81) As CellData

Type CellPanel_Data(FirstCell As Int, Completed As Boolean, HighLighted As Boolean, Correct As Boolean)
Dim CellPanel_Info(9) As CellPanel_Data

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim lblCell(81) As Label
Dim GridPanel(9) As Panel
Dim su As StringUtils

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim TextSizeBasedOnWidth As Int
Dim CalculatedTextSize As Int
Dim RowHeight As Int = 10%x

Dim pnlDisplayPanel As Panel

'==================================================================
'Add the Display Panel to Activity
'==================================================================
pnlDisplayPanel.Initialize("DisplayPanel")
pnlDisplayPanel.Color = Colors.Black
Activity.AddView(pnlDisplayPanel, 0, 0, 100%x, 100%y)

'==================================================================
'Add the Nine Grid Panels to the Display Panel
'==================================================================
For Qty = 0 To 8
GridPanel(Qty).Initialize("GridPanel"&Qty)
GridPanel(Qty).Color = Colors.Black 'ColorInfo.Cell_Panel_Background
Next

Dim TitleBarHeight As Int
TitleBarHeight = 20%x

pnlDisplayPanel.AddView(GridPanel(0), 3.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(3), 3.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(6), 3.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(1), 34.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(4), 34.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(7), 34.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(2), 65.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(5), 65.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(8), 65.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)

'==================================================================
'Add a Corner Radius to the Eighty One Cells, if desired
'==================================================================
Dim SmallColor(81) As ColorDrawable
Dim CornerRadius As Int
CornerRadius = 2 'radius goes here
For Qty = 80 To 0 Step -1
SmallColor(Qty).Initialize(Colors.RGB(255, 140, 0), CornerRadius) '(ColorInfo.Cell_Unprotected, CornerRadius)
Next
'==================================================================
'Add the Eighty One Cells, Nine each the Nine Grid Panels
'==================================================================

For Qty = 80 To 0 Step -1
lblCell(Qty).Initialize("Cells")
Dim JO As JavaObject = lblCell(Qty)
JO.RunMethod("setPadding",Array As Object(0,0,0,0))
lblCell(Qty).Background = SmallColor(Qty)
lblCell(Qty).Tag = Qty
lblCell(Qty).Gravity = Bit.Or(Gravity.Center_Vertical, Gravity.Center_Horizontal)
Next

For Amount = 0 To 2
For Kount = 0 To 2
For Count = 0 To 2
For Qty = 0 To 2
GridPanel(Amount*3+Kount).AddView(lblCell(Amount*27+Kount*9+Count*3+Qty), Qty*10%x+1%X, Count*RowHeight+1%x, 9%x, 9%x)
CellPanel_Info(Amount*3+Kount).FirstCell = Amount*27+Kount*9
CellInfo(Amount*27+Kount*9+Count*3+Qty).Column = Kount*3+Qty
CellInfo(Amount*27+Kount*9+Count*3+Qty).Row = Amount*3+Count
CellInfo(Amount*27+Kount*9+Count*3+Qty).Group = Amount*3+Kount
Next
Next
Next
Next

TextSizeBasedOnWidth = Width_Of_Text("9", lblCell(80))
CalculatedTextSize = Height_Of_Text(lblCell(80), "9", TextSizeBasedOnWidth, "S")

lblCell(80).TextSize = CalculatedTextSize

'If you leave the following instruction uncommented, the application will crash.
'If you comment out the following instruction, the application will run but Cell(80) will remain Square.
'I have no idea why. PLEASE HELP.
SER(lblCell(80))

For Qty = 79 To 0 Step -1
lblCell(Qty).TextSize = CalculatedTextSize
SER(lblCell(Qty))
Next

End Sub

Sub SER(V As View)
SCR(V,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x)
End Sub

Sub SCR(v As View, Rx_TopLeft As Float, Ry_TopLeft As Float, Rx_TopRight As Float, Ry_TopRight As Float, Rx_BottomRight As Float, Ry_BottomRight As Float, Rx_BottomLeft As Float, Ry_BottomLeft As Float)
Dim jo As JavaObject = v.Background
If v.Background Is ColorDrawable Or v.Background Is GradientDrawable Then
jo.RunMethod("setCornerRadii", Array As Object(Array As Float(Rx_TopLeft, Ry_TopLeft, Rx_TopRight, Ry_TopRight, Rx_BottomRight, Ry_BottomRight, Rx_BottomLeft, Ry_BottomLeft)))
End If
End Sub


Sub Width_Of_Text(Text_to_Size As String, TestLabel As Label) As Int

Dim TextSizeBasedOnWidth As Int
Dim CVS As Canvas
CVS.Initialize(TestLabel)

For Qty = 60 To 0 Step -1
TextSizeBasedOnWidth = CVS.MeasureStringWidth(Text_to_Size, TestLabel.Typeface, Qty)
If TextSizeBasedOnWidth < TestLabel.Width Then
Exit
End If
Next
Return(Qty)

End Sub

Sub Height_Of_Text( TestLabel As Label, Text_to_Size As String, TextSizeBasedOnWidth As Int, SorM As String) As Int

Dim CalculatedTextHeight As Int

For Qty = 60 To 5 Step -1
TestLabel.TextSize = Qty
CalculatedTextHeight = su.MeasureMultilineTextHeight(TestLabel, Text_to_Size)
If SorM = "S" Then
If CalculatedTextHeight < TestLabel.Height Then
If TextSizeBasedOnWidth < Qty Then
Qty = TextSizeBasedOnWidth
End If
Exit
End If
Else If SorM = "M" Then
If CalculatedTextHeight < TestLabel.Height Then
Exit
End If
End If
Next

Return(Qty)

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
[/CODE]

Thank you
Gavin.

PS
I originally used the following code to set the Cell Background.

B4X:
    Dim SmallColor    As     ColorDrawable
    Dim    CornerRadius    As    Int
    CornerRadius = 2    'radius goes here
    '==================================================================
    'Add the Eighty One Cells, Nine each the Nine Grid Panels
    '==================================================================
    
    For Qty = 80 To 0 Step -1
        lblCell(Qty).Initialize("Cells")
        Dim JO As JavaObject = lblCell(Qty)
        JO.RunMethod("setPadding",Array As Object(0,0,0,0))
        lblCell(Qty).Background = SmallColor
        lblCell(Qty).Tag = Qty
        lblCell(Qty).Gravity = Bit.Or(Gravity.Center_Vertical, Gravity.Center_Horizontal)
    Next

The result is the same regardless of the way "SmallColor" is dimensioned.
 
You have neglected to mention, or provide the log text of, what error is occurring.

Sorry Jeffrey, you are correct.
Here is the complete log of the error.

Logger connected to: 015d3f18d134200e
--------- beginning of system--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 134 (Main)
java.lang.RuntimeException: Method: setCornerRadii not found in: android.graphics.drawable.BitmapDrawable
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at b4a.SetCornrRadiiExample.main._scr(main.java:810)
at b4a.SetCornrRadiiExample.main._ser(main.java:771)
at b4a.SetCornrRadiiExample.main._activity_create(main.java:636)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.SetCornrRadiiExample.main.afterFirstLayout(main.java:104)
at b4a.SetCornrRadiiExample.main.access$000(main.java:17)
at b4a.SetCornrRadiiExample.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
** Activity (main) Resume **

Gavin
 
Upvote 0
In the future, please use the code tags when posting logs.

java.lang.RuntimeException: Method: setCornerRadii not found in: android.graphics.drawable.BitmapDrawable
Here is the problem. The radii method only exists for color and gradient drawables, not bitmap drawables. At some point you either used the ".Color" property or set a bitmap to the background.
 
Upvote 0
In the future, please use the code tags when posting logs.


Here is the problem. The radii method only exists for color and gradient drawables, not bitmap drawables. At some point you either used the ".Color" property or set a bitmap to the background.

Jeffrey, thank you for the advice when posting logs.

Yes, I was aware that setCornerRadii only worked for color and gradient drawables.
Now, with you help, I understand the that the error message was telling me that I had used a bitmap, BUT WHERE?

If you look at the code posted in the PS of my first posting, you can see where I set the "ColorDrawable" and in the For/Next loop, set the backgrounds.

All looks fine to me but, if I add the line "lblCell(80).Background = SmallColor" just above "SER(lblCell(80))", then the program runs without crashing so, Where is this .color or bitmap sneaking into the works. (LOL)

Got me beat,(for the moment).

Gavin
 
Upvote 0
Well, it has me stumped.

The following code is almost identical to the code posted above with the exception that I now set the Background, TextSize, etc "AFTER" I add the labels to the "GridPanel".
Now, the program does not crash.
I have modified the original program in the same manner with the same result, no crash.

This, I can not explain.

At this point in time, I do not wish to declare this problem solved.

As requested in the above, any ideas greatly appreciated.

[CODE lang="b4x" title="setCornerRadii Example" highlight="106"]#Region Project Attributes
#ApplicationLabel: Set Corner Radii
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: True
#IncludeTitle: False
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type CellData (Security As String, RCC As String, Row As Int, Column As Int, Group As Int, LastEntry As String, _
Klicked As Int, HighLighted As Boolean, Following As String, Swapable As Boolean, SwapWith As String, _
SwapShape As String, ReadyToSwap As Boolean)
Dim CellInfo(81) As CellData

Type CellPanel_Data(FirstCell As Int, Completed As Boolean, HighLighted As Boolean, Correct As Boolean)
Dim CellPanel_Info(9) As CellPanel_Data

End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim lblCell(81) As Label
Dim GridPanel(9) As Panel
Dim su As StringUtils

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim TextSizeBasedOnWidth As Int
Dim CalculatedTextSize As Int
Dim RowHeight As Int = 10%x

Dim pnlDisplayPanel As Panel

'==================================================================
'Add the Display Panel to Activity
'==================================================================
pnlDisplayPanel.Initialize("DisplayPanel")
pnlDisplayPanel.Color = Colors.Black
Activity.AddView(pnlDisplayPanel, 0, 0, 100%x, 100%y)

'==================================================================
'Add the Nine Grid Panels to the Display Panel
'==================================================================
For Qty = 0 To 8
GridPanel(Qty).Initialize("GridPanel"&Qty)
GridPanel(Qty).Color = Colors.Blue'Black 'ColorInfo.Cell_Panel_Background
Next

Dim TitleBarHeight As Int
TitleBarHeight = 20%x

pnlDisplayPanel.AddView(GridPanel(0), 3.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(3), 3.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(6), 3.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(1), 34.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(4), 34.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(7), 34.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(2), 65.5%x, TitleBarHeight+1.0%x+ 00%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(5), 65.5%x, TitleBarHeight+1.0%x+ 31%x, 31%x, 31%x)
pnlDisplayPanel.AddView(GridPanel(8), 65.5%x, TitleBarHeight+1.0%x+ 62%x, 31%x, 31%x)

'==================================================================
'Add a Corner Radius to the Eighty One Cells, if desired
'==================================================================
Dim SmallColor As ColorDrawable
Dim CornerRadius As Int
CornerRadius = 2 'radius goes here
SmallColor.Initialize(Colors.RGB(255, 140, 0), CornerRadius) '(ColorInfo.Cell_Unprotected, CornerRadius)

'==================================================================
'Add the Eighty One Cells, Nine each the Nine Grid Panels
'==================================================================
For Qty = 80 To 0 Step -1
lblCell(Qty).Initialize("Cells")
Next

For Amount = 0 To 2
For Kount = 0 To 2
For Count = 0 To 2
For Qty = 0 To 2
GridPanel(Amount*3+Kount).AddView(lblCell(Amount*27+Kount*9+Count*3+Qty), Qty*10%x+1%X, Count*RowHeight+1%x, 9%x, 9%x)
CellPanel_Info(Amount*3+Kount).FirstCell = Amount*27+Kount*9
CellInfo(Amount*27+Kount*9+Count*3+Qty).Column = Kount*3+Qty
CellInfo(Amount*27+Kount*9+Count*3+Qty).Row = Amount*3+Count
CellInfo(Amount*27+Kount*9+Count*3+Qty).Group = Amount*3+Kount
Next
Next
Next
Next

TextSizeBasedOnWidth = Width_Of_Text("9", lblCell(80))
CalculatedTextSize = Height_Of_Text(lblCell(80), "9", TextSizeBasedOnWidth, "S")

For Qty = 80 To 0 Step -1
lblCell(Qty).Background = SmallColor
lblCell(Qty).TextSize = CalculatedTextSize
lblCell(Qty).Text = Qty Mod 9 + 1
Dim JO As JavaObject = lblCell(Qty)
JO.RunMethod("setPadding",Array As Object(0, 0, 0, 0)) '(15,15,15,15))
lblCell(Qty).Tag = Qty
lblCell(Qty).Gravity = Bit.Or(Gravity.Center_Vertical, Gravity.Center_Horizontal)
SER(lblCell(Qty))
Next

End Sub

Sub SER(V As View)
SCR(V,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x,4.5%x)
End Sub

Sub SCR(v As View, Rx_TopLeft As Float, Ry_TopLeft As Float, Rx_TopRight As Float, Ry_TopRight As Float, Rx_BottomRight As Float, Ry_BottomRight As Float, Rx_BottomLeft As Float, Ry_BottomLeft As Float)
Dim jo As JavaObject = v.Background
If v.Background Is ColorDrawable Or v.Background Is GradientDrawable Then
jo.RunMethod("setCornerRadii", Array As Object(Array As Float(Rx_TopLeft, Ry_TopLeft, Rx_TopRight, Ry_TopRight, Rx_BottomRight, Ry_BottomRight, Rx_BottomLeft, Ry_BottomLeft)))
End If
End Sub


Sub Width_Of_Text(Text_to_Size As String, TestLabel As Label) As Int

Dim TextSizeBasedOnWidth As Int
Dim CVS As Canvas
CVS.Initialize(TestLabel)

For Qty = 60 To 0 Step -1
TextSizeBasedOnWidth = CVS.MeasureStringWidth(Text_to_Size, TestLabel.Typeface, Qty)
If TextSizeBasedOnWidth < TestLabel.Width Then
Exit
End If
Next
Return(Qty)

End Sub

Sub Height_Of_Text( TestLabel As Label, Text_to_Size As String, TextSizeBasedOnWidth As Int, SorM As String) As Int

Dim CalculatedTextHeight As Int

For Qty = 60 To 5 Step -1
TestLabel.TextSize = Qty
CalculatedTextHeight = su.MeasureMultilineTextHeight(TestLabel, Text_to_Size)
If SorM = "S" Then
If CalculatedTextHeight < TestLabel.Height Then
If TextSizeBasedOnWidth < Qty Then
Qty = TextSizeBasedOnWidth
End If
Exit
End If
Else If SorM = "M" Then
If CalculatedTextHeight < TestLabel.Height Then
Exit
End If
End If
Next

Return(Qty)

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
[/CODE]

Gavin
 
Upvote 0
Back
Top