Android Question How to get the parent's color?

Theera

Expert
Licensed User
Longtime User
My code is belows:
B4X:
Public Sub AddToParent(Parent As Panel, Left As Int, Top As Int, Width As Int, Height As Int)
    Parent.AddView(mBase, Left, Top, Width, Height)
    cvs.Initialize(mBase)
    
    ' Set parent color based on parent background
    Try
        If Parent Is Panel Then
            Dim p As Panel = Parent
            mParentColor = p.color       '<==Error code
        End If
    Catch
        Log("Could not get parent panel color")
    End Try
End Sub
 

Theera

Expert
Licensed User
Longtime User
Thank you for replies . mParentColor is int, and p.color is int ,too ,but p.color is write only,so I can't solved it.
B4X:
Sub Class_Globals
    ....
    ....
    ' Parent color for background
    Private mParentColor As Int = xui.Color_White
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
How is mParentColor defined? Types must match!
I'm ok. I found the old code may be solved the problem. Thank you,Cableguy
B4X:
'In CustomView Class
Public Sub GetParentColorIs As Int
    Try
        If mBase.Parent=Null Then Return xui.Color_White
        Dim jo As JavaObject=mBase.Parent
      
        'Check field color
        If HasField(jo,"color") Then
            Return jo.GetField("color")
        End If
      
    Catch
        Log("Get parent color failed" & LastException.Message)
    End Try
    Return xui.Color_White
End Sub

'Check Field

Private Sub HasField(jo As JavaObject,fieldName As String) As Boolean
     Try
         Dim cls As JavaObject=jo.GetField("java.lang.Object.class")
        Dim fields() As Object= cls.RunMethod("getDeclaredFields",Null)
      
        For Each field As JavaObject In fields
            Dim name As String=field.RunMethod("getName",Null)
            If name=fieldName Then
                 Return True
            End If
        Next
    Catch
        Log(LastException)
      
    End Try
    Return False
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
1749718697539.png


The color attribute of a panel is WRITE only, meaning you cannot read it... you will need to come up with a different way to retrieve it... like retrieving a pixel color
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
In what situation you want to get the Parent's colour?
Are you creating a custom view?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
In what situation you want to get the Parent's colour?
Are you creating a custom view?
Yes, I've tried to create CustomView. Assume If I paste the customview on the panel or non-panel , how to get the parent's color. But I don't make sure what strategy get
be perfectly.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I think that you are not showing all the code of the AddToParent routine in the first post.
You add mBase, but it is not initialized or created.
You should use B4XPages and as much as possible B4XViews.

B4X:
Public Sub AddToParent(Parent As B4XView, Left As Int, Top As Int, Width As Int, Height As Int)
    mBase = xui.CreatePanel("")
    Parent.AddView(mBase, Left, Top, Width, Height)
    cParentColor = Parent.Color
End Sub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Upvote 0

aeric

Expert
Licensed User
Longtime User
That's my point too, with B4XView, we can access the Color properties.
No need so complicated.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
With B4XPages, you can add views only on Panels.
Therefore no need to check if it is a Panel.
I'm studying the customview about progressbar. The percent of number is on the panel, or not, I can't predict, so I must check theproject has panel, ornot.I don't understand your code at the line why we have to create again.If the project doesn't have panel, how to code it.
mBase = xui.CreatePanel("")
 
Upvote 0

Jerryk

Active Member
Licensed User
Longtime User
I'm ok. I found the old code may be solved the problem. Thank you,Cableguy
B4X:
'In CustomView Class
Public Sub GetParentColorIs As Int
    Try
        If mBase.Parent=Null Then Return xui.Color_White
        Dim jo As JavaObject=mBase.Parent
     
        'Check field color
        If HasField(jo,"color") Then
            Return jo.GetField("color")
        End If
     
    Catch
        Log("Get parent color failed" & LastException.Message)
    End Try
    Return xui.Color_White
End Sub

'Check Field

Private Sub HasField(jo As JavaObject,fieldName As String) As Boolean
     Try
         Dim cls As JavaObject=jo.GetField("java.lang.Object.class")
        Dim fields() As Object= cls.RunMethod("getDeclaredFields",Null)
     
        For Each field As JavaObject In fields
            Dim name As String=field.RunMethod("getName",Null)
            If name=fieldName Then
                 Return True
            End If
        Next
    Catch
        Log(LastException)
     
    End Try
    Return False
End Sub
I tried your HasField function and got an error on line
B4X:
  Dim cls As JavaObject=jo.GetField("java.lang.Object.class")
error: java.lang.RuntimeException: Field: java.lang.Object.class not found in: anywheresoftware.b4a.BALayout
Am I missing something in the manifest, jar, etc?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your code you have this line:
B4X:
Parent.AddView(mBase, Left, Top, Width, Height)
What is mBase for you ?
Where do you declare and initialize mBase ?
To be able to better help you, you must post your project, so we can see what you have done.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
To be able to better help you
Am I missing something?? The OP already declared the thread as solved.... so new questions should result in new threads, right?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
In your code you have this line:
B4X:
Parent.AddView(mBase, Left, Top, Width, Height)
What is mBase for you ?
Where do you declare and initialize mBase ?
To be able to better help you, you must post your project, so we can see what you have done.
My CustomView is from SDDust's liquidProgress gress inspiration. I think that mBase can be Activity (As Non-CrossPlatforms) or B4XPage.main (As CrossPlatforms)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I see many developers like to add custom views by code but as I understand it is supposedly added in layout designer.
 
Upvote 0
Top