Android Question Puzzling... me

Cableguy

Expert
Licensed User
Longtime User
Hi guys

Continuing my quest to get AnyView.Parent and use it to set as the parent of another view I came to these lines of code...

B4X:
  Private JPanel = DPanel As JavaObject 'DPanel is a Panel, JPanel is the JObj linked to it
   Private jParent = byParent As JavaObject 'byParent is the view passed in the class initializer, JParent is its JObj
 
   JPanel.RunMethod("setParent",jParent.RunMethod("getParent",Null))

wich doesn't work...

I am trying to set a views Parent to be another's view Parent

if needed i can provide the debug stack
 
Last edited:

canalrun

Well-Known Member
Licensed User
Longtime User
Hi guys

Continuing my quest to get AnyView.Parent and use it to set as the parent of another view I came to these lines of code...

Erel gave me this code a while ago. SetParent is probably pretty similar.

B4X:
Sub GetParent(v As View) As View
  Dim r As Reflector
  r.Target = v
  Return r.RunMethod("getParent")
End Sub

I'm not sure if it is possible to SetParent. Others may know.

Barry.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I've been fidling with reflector but I still can=t get it right... but I think I'm getting closer as now it claims that the view I pass in the initializer is not found by the .RunMethod

B4X:
java.lang.RuntimeException: Field: Target not found in: android.widget.Button
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
taking it step by step, .RunMethod("getParent") returns me
B4X:
anywheresoftware.b4a.BALayout@2101edc8

how can I use this info to set it as a views parent?

P.S. Browsing the Button class I found this

public void setParent (View root, int virtualDescendantId)
Added in API level 16
Sets the parent to be a virtual descendant of the given root. If virtualDescendantId equals to NO_ID the root is set as the parent.

A virtual descendant is an imaginary View that is reported as a part of the view hierarchy for accessibility purposes. This enables custom views that draw complex content to report them selves as a tree of virtual views, thus conveying their logical structure.


Note: Cannot be called from an AccessibilityService. This class is made immutable before being delivered to an AccessibilityService.


Parameters
root The root of the virtual subtree.
virtualDescendantId The id of the virtual descendant.

seems that setParent expects more than just a Parent view...
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Why not use RemoveView and AddView?
This is to be part of a class whos initialiser takes a view as parameter.
My "custom view" is to be added to this view's parent. So remove parent is not necessary and addView is impossible to use since I still do not know which will be the parent of my view and of which type, a panel or activity.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
C'mon guys, this should be easy for your brilliant coding minds...

Imagine a class, within it, a button... the initialiser of this class as the activity passed to it as a parameter... this class button click event does a series of checks and then should call another class wich in turn will add a Panel to the calling Button's Parent...

this is why I need to find out how setParent based on a getParent method...

Help!!!!
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks Erel... So much simpler.... One last question...if I am not sure of button1 parent, as in if it is a Panel or activity, how can I proof it?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
This routine checks if a view is an Activity.
B4X:
' Checks if a view is an Activity
Sub IsActivity(v As View) As Boolean
    Try
        v.Left = 10dip
        Return False
    Catch
        Return True
    End Try
End Sub
B4A GetType returns 'anywheresoftware.b4a.BALayout' for both Activity and Panel.
The code above is a workaround.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks Klaus... who else would have thought of check if a view was resizable (Ooops... I mean... offsetable... I think..).. very simple workaround...
Did I already said Thanks?
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Hi.... yeah... it's me again....sorry...

using the above suggestions I came to create the following condition

B4X:
  If IsActivity(GetParent(byParent)) = True Then
     Private Parent As Activity = GetParent(byParent)
   Else
     Private Parent As Panel = GetParent(byParent)
   End If

but the IDE claims it to be invalid

Error description: Current declaration does not match previous one.
Previous: {Type=Activity,Rank=0, RemoteObject=True}
Current: {Type=Panel,Rank=0, RemoteObject=True}
Occurred on line: 13
Private Parent As Panel = GetParent(byParent)

if, I am conditioning my declaration to the result of a sub, why does it not validate my code?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
so I came to this simplified code...

B4X:
  Private Parent = GetParent(byParent) As activity
   DPanel.Initialize("")
   Parent.AddView(DPanel,100dip, 50dip, 300dip, 300dip)

wich does not error out, BUT... apparently does nothing...
(I suspect it adds the DPanel to an unexistent activity)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
this had passed by my attention...

logging GetParent I get (BALayout): Layout not available
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The byParent is a Button view... I tried both adding it by code as in the designer...
I just can't figure out what is wrong... A few days ago GetParent was giving me a completly diferent log, as shown inone of my previous post...
What exactly do you mean with "have layouts"?
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
So... restarting from scrap, adding a few logs here and there...

My class initializer looks like this...
(the class is initialized from a Button1_Click and passes itself as argument)

B4X:
Public Sub Initialize(byParent As View) As View
   Log(byParent)
   Private jParent = byParent As JavaObject 'byParent is the view passed in the class initializer, JParent is its JObj
   Log(jParent.RunMethod("getParent",Null))
   Dim Parent As Panel = jParent.RunMethod("getParent",Null)
   Log(Parent)
   DPanel.Initialize("")
   Parent.AddView(DPanel,10dip,10dip,300dip,100dip)
End Sub

my debug and logs...

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
(Button): Left=150, Top=150, Width=150, Height=150, Tag=
anywheresoftware.b4a.BALayout{41886448 V.E..... ........ 0,0-480,672 #1}
(BALayout): Layout not available

WHY??? Layout NOT available? WTF????
 
Upvote 0
Top