'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim fromx,fromy,tox,toy As Int
   
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 Obj1 As Reflector
   Dim Obj2 As Reflector
   
   
   Dim EditText1 As EditText
   Dim EditText2 As EditText
   Dim Panel1 As Panel
    Dim picCanvas As Canvas
   Dim Button2 As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
   
   Dim DestRect As Rect
   Activity.LoadLayout("Layout")
   
   EditText1.Top=Panel1.Top
   EditText1.Left=Panel1.Left
   EditText1.Width=Panel1.Width
   EditText1.Height=Panel1.Height
   
   
   fromx=0
   fromy=0
   tox=0
   toy=0
   
   picCanvas.Initialize(Panel1)
   DestRect.Initialize(0dip, 0dip, 100%x, 100%y)
   picCanvas.DrawRect(DestRect, Colors.Transparent, True, 1dip)
   Panel1.Invalidate
   
   ' sets events on EditText1 using the common View events in the Reflection library
   Obj1.Target = EditText1 
   EditText1.Tag = "EditText1"
   
   Obj1.SetOnTouchListener("OnTouch")
   Obj1.SetOnFocusListener("OnFocus")
   Obj1.SetOnKeyListener("OnKey")   
   
   ' sets the same events on EditText2 using the generalised Proxy in the Reflection library
   Obj1.Target = EditText2 
   EditText2.Tag = "EditText2"
   
   ' specify the interfaces whose events we want to catch
   Dim ifaces(3) As String
   ifaces(0) = "android.view.View$OnTouchListener"
   ifaces(1) = "android.view.View$OnFocusChangeListener"
   ifaces(2) = "android.view.View$OnKeyListener"
   
   ' get the proxy that implements those interface and the Sub to be called
   Obj2.Target = Obj2.GetProxy(ifaces, "OnProxy")
   
   Dim args(1) As Object
   ' the proxy instance is the argument for all the "setOnXxxxListener" calls
   args(0) = Obj2.Target
   
   Dim types(1) As String
   ' set the interface type of the argument to the appropriate one for each call
   types(0) = "android.view.View$OnTouchListener"
   Obj1.RunMethod4("setOnTouchListener", args, types)
   types(0) = "android.view.View$OnFocusChangeListener"
   Obj1.RunMethod4("setOnFocusChangeListener", args, types)
   types(0) = "android.view.View$OnKeyListener"
   'I've had to comment this line out as there seems to be a bug in Android
   'that causes an Exception when closing the app when a Proxy is used on this EditText interface
   'Obj1.RunMethod4("setOnKeyListener", args, types)
   
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub OnProxy(method As String, args() As Object) As Object
   Dim action, ix, iy As Int
   Dim viewtag, msg As String
   Obj1.Target = args(0) ' args(0) is the view
   viewtag = Obj1.RunMethod("getTag")
   If method = "onTouch" Then
      Obj1.Target = args(1) ' args(1) is a Motion Event   
     ' ACTION_DOWN = 0; ACTION_UP = 1; ACTION_MOVE = 2;   
      action = Obj1.RunMethod("getAction")
      ix = Obj1.RunMethod("getX")
      iy = Obj1.RunMethod("getY")
   End If   
   Return False
End Sub
Sub OnTouch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
   Dim ix, iy As Int
   ix = X
   iy = Y
  ' ACTION_DOWN = 0; ACTION_UP = 1; ACTION_MOVE = 2;   
  '   EditText3.Text = "x=" & ix & " " & "y=" & iy
  '=============================Line drawing here ===============================
   If action=0 Then 
   'puts a circle if the user just touches the screen briefly
    fromx=X
    fromy=Y
    picCanvas.DrawCircle(fromx, fromy, 10dip, Colors.Red, True, 1dip)
     Panel1.Invalidate   
   End If   
  
     If action=1 Then 
   'puts final circle as the user lifts their finger off the screen
    tox=X
    toy=Y
    picCanvas.DrawCircle(tox, toy, 10dip, Colors.Red, True, 1dip)
     Panel1.Invalidate   
   End If  
  
   If action=2 Then 
    'Filled circles stop jaggies between lines
    tox=X
    toy=Y
    picCanvas.DrawCircle(fromx, fromy, 10dip, Colors.Red, True, 1dip)
    picCanvas.DrawLine(fromx,fromy,tox,toy,Colors.Red,20dip)
    picCanvas.DrawCircle(tox, toy, 10dip, Colors.Red, True, 1dip)
    fromx=tox
    fromy=toy
    
     Panel1.Invalidate   
   End If 
    
   ' ToastMessageShow(viewtag & " Touch : " & action & " " & ix & " " & iy, False)
   Return False
End Sub
Sub OnKey(viewtag As Object, keycode As Int, keyevent As Object) As Boolean
   Return False
End Sub
Sub OnFocus(viewtag As Object, focus As Boolean)
'   ToastMessageShow(viewtag & " Focus : " & focus, False)
End Sub
Sub Button2_Click
   
  'Clears the drawing
   
            Dim DestRect As Rect
            DestRect.Initialize(0dip, 0dip, 100%x, 100%y)            
            picCanvas.DrawRect(DestRect, Colors.Transparent, True, 1dip)
            Panel1.Invalidate
   
End Sub