Android Question access an activity view from a class ?

aeropic

Active Member
Licensed User
Longtime User
Hi,

I'm using cameraEX class in which the focus done triggers an event (Camera_FocusDone)

I would want to modify the main activity layout according to the success of focus done event. I want to make visible a panel (PnlFocus)

When I try this code, it fails:
B4X:
Private Sub Camera_FocusDone (Success As Boolean)
    If Success Then
        If shoot Then TakePicture
      
        Main.PnlFocus.visible = True  '<====== FAILS HERE
    Else
        Log("AutoFocus error.")
        ToastMessageShow("unable to focus...", False)
        'TakePicture
    End If
End Sub

I there a proper way to do this ?
Thanks for your help
 
Last edited:

Kjell

Member
Licensed User
Longtime User
You write:
I would want to modify the main activity layout according to the success of focus done event. I want to make visible a panel (PnlFocusDone)

But the code sais:

B4X:
 Main.PnlFocus.visible = True  '<====== FAILS HERE

Should it be?

B4X:
 Main.PnlFocusDone.visible = True
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,
not yet used the CameraEx class, but if, as I believe, your Sub Camera_FocusDone is part of the class you could call a specialized sub in Main where you do all the settings you need.
I mean something like:
B4X:
'in camera class sub
PrivateSub Camera_FocusDone (Success AsBoolean)
If Success Then
  If shoot Then TakePicture
  CallSubDelayed2(main, "ShowPanel", True)
  ...
end sub

'in main
Sub ShowPanel(show as boolean)
PnlFocus.visible = show
end sub

Or you can pass on class Initialize (or even to a different sub) reference to the calling Activity and use that to reach its elements like CallingActivity.GetView(x)....

Umberto
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In general, you can pass objects to classes, including Activities.

In addition, you can call a method of TargetModule caller (the activity, in this case) using CallSubDelayed (look for it on the site).

In this case, I imagine that the Panel1 you pass to the initialization is the panel that you want to manage. So, you can create a global private variable, for example mActPanel, and set:
mActPanel = Panel1 in the Initialize; then:
B4X:
Private Sub Camera_FocusDone (Success As Boolean)
    If Success Then
        If shoot Then TakePicture
     
        mActPanel = True  '<====== FAILS HERE
    Else
        Log("AutoFocus error.")
        ToastMessageShow("unable to focus...", False)
        'TakePicture
    End If
End Sub

(Umberto was faster :))
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Better yet:

In the Class
B4X:
Private Sub Camera_FocusDone (Success As Boolean)
    If Success Then
        TakePicture
        If SubExists(target, event & "_" & "FocusDone") Then
            CallSubDelayed(target, event & "_" & "FocusDone")
        End If
    Else
        Log("AutoFocus error.")
    End If
End Sub


In the Activity
B4X:
Private Sub Camera1_FocusDone
    PnlFocus.Visible = False
End Sub
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
Thanks a lot for your helpfull answers.

I finished with Luca's solution adding a panel in the CameraEX class Initialize sub :
B4X:
Public Sub Initialize (Panel1 As Panel, FrontCamera As Boolean, TargetModule As Object, EventName As String, panelfocus As Panel)
    target = TargetModule
    event = EventName
    Front = FrontCamera
    pFocus = panelfocus
coupled with the pFocus panel declared as global to the class, it is exactly what I was searching for.

The event raising option is very interesting as it could be, indeed, a better way to decouple the class and the main...

@udg : the callsubdelay is of course another solution
@kjll : you're right my question was misleading, there was a typo in (PnlFocusDone) ==> I edited the post to (PnlFocus)
 
Upvote 0
Top