Android Question [Solved] Find the Activity from a View

Star-Dust

Expert
Licensed User
Longtime User
I need to get the Activity from the single object using the parent method.
I would like to use part method recursively up to climb to activity.

I used this code thinking it worked, but I was wrong:

B4X:
Dim Obj As View = Button1
  
Do While Not(Obj.Parent Is Activity)
    Obj=Obj.Parent
Loop
MyAct=Obj

Any suggest?
 

Star-Dust

Expert
Licensed User
Longtime User
Thanks @LucaMs for suggestion but does not change the result.

I think I'm wrong about the basic concept.

Here's what I want to get. I need to add in the objects' activity.
Activity.AddViev ()

But I would like to do it within a class.
To do this, I thought about going back to activity starting from a label (which I can pass on as a class) to recursively recapture up to activity.

But I think the basic concept is wrong. Why I confuse activity with the view of activity.

With this method I can do the most to get to the labeling panel but can not get up.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I removed my post before you answered, because it wasn't enough.

If I understand well you are creating a class; why don't you pass an Activity to this class as parameter (inside the initialization or as property)?

And why we are "talking" in English if we speak italian better and exists an Italian Forum? ;)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thank you very much for your suggestion.

The class is a customview. I am creating a Spinner object.
I need you to open a ListView after clicking the spinner object.
The ListView must be anchored to activity.
The panel containing the spinner may be small dimensions and do not contain the listview; i need to add the listview to the activity.

For the moment I'm using this code.
It assumes that the label is linked to Activity or a panel that in turn is linked to activity.
It does not work if there are more nested panels

B4X:
MyAct=mBase.Parent
Left=mBase.Left
Top=mBase.Top
If MyAct.Parent<> Null Then
   Try
     Left=Left+MyAct.Left
     Top=Top+MyAct.Top
   Catch
      Log("Errore")
    End Try
    MyAct=MyAct.Parent
End If

I need you to open a ListView after clicking the spinner object.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Use Custom View with Designer support.

You can resize its base panel (mBase) at runtime, to show or hide the ListView.

(Anyway I found your previous "error": Is Activity returns True even if you compare a Panel. To know if a View is an Activity, you should use: If GetType(ViewObject) = "android.widget.FrameLayout")
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I already use Design, but I'm afraid if the spinner was inside a small dimension, it would have a problem with the listview even though mBase resizations. It would always be bound to the panel to which it is anchored.

But I thank you enormously for the suggestion to see if it is an activity. Now works.
Great @LucaMs ;););)
B4X:
Dim Obj As View = mBase
Do While Not(GetType(Obj) = "android.widget.FrameLayout")
    Try
        Left=Left+Obj.Left
        Top=Top+Obj.Top
    Catch
        Log("Errore")
    End Try
    Obj=Obj.Parent
Loop
MyAct=Obj
 
Last edited:
Upvote 1

Filippo

Expert
Licensed User
Longtime User
I already use Design, but I'm afraid if the spinner was inside a small dimension, it would have a problem with the listview even though mBase resizations. It would always be bound to the panel to which it is anchored.

But I thank you enormously for the suggestion to see if it is an activity. Now works.
Great @LucaMs ;););)
B4X:
Dim Obj As View = mBase
Do While Not(GetType(Obj) = "android.widget.FrameLayout")
    Try
        Left=Left+Obj.Left
        Top=Top+Obj.Top
    Catch
        Log("Errore")
    End Try
    Obj=Obj.Parent
Loop
MyAct=Obj
Hi @Star-Dust ,
I know it's an old thread, but I was looking for such a solution and found your code.

Here is my modified version:
B4X:
Private Sub FindActivity(v As View) As Activity
    Dim Left, top As Int
    Dim Obj As View = v
    Do While Not(GetType(Obj) = "android.widget.FrameLayout")
        Obj = Obj.Parent
        Try
            Left = Left + Obj.Left
            top = top + Obj.Top
        Catch
            Log("Errore")
            Exit
        End Try
    Loop
    Return Obj
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
In the meantime I have found others ?
(Inzwischen habe ich andere gefunden ?)
B4X:
Sub getCurrentActivity As Activity
   Dim r As Reflector
   r.Target = r.GetActivityBA
   Return r.GetField("vg")  
End Sub

B4X:
'Returns the root view of the given view, it's mainly the Activity
Sub getRootView(v As View) As View
    Dim jo = v As JavaObject
    Return jo.RunMethod("getRootView", Null)
End Sub
 
Upvote 0
Top