Here is a simple example of how to support dragging a panel of buttons or other views like EditText around the screen.
I extracted the code from one of my apps and added detailed comments. It works by placing a transparent panel over the screen and then manipulating the views underneath. The demo screen format was taken from one of Erel's examples.
It uses the Reflection library to send events to other views and the IME library for the EditText box to show the SoftKeyboard.
I have tested the example on Android 2.2, 2.3 and 4.03.
I did not include the LongClick code as it makes the example less clear. Other types of views can also be easily added.
I hope this will prove useful to both newcomers and existing forum users.
bluejay
I extracted the code from one of my apps and added detailed comments. It works by placing a transparent panel over the screen and then manipulating the views underneath. The demo screen format was taken from one of Erel's examples.
It uses the Reflection library to send events to other views and the IME library for the EditText box to show the SoftKeyboard.
I have tested the example on Android 2.2, 2.3 and 4.03.
I did not include the LongClick code as it makes the example less clear. Other types of views can also be easily added.
B4X:
' Demo program to show a draggable panel with buttons and edittext
' bluejay July,2012
' uses Reflection library and IME library
Sub Process_Globals 'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
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 TransparentPanelOnTop As Panel
Dim BottomPanel As Panel ' views that will be made draggable
Dim Button1 As Button
Dim Button2 As Button
Dim Label1 As Label
Dim TxtEdit As EditText
Dim StartX As Float ' used only for panel dragging
Dim StartY As Float
Dim LastX As Float
Dim LastY As Float
Dim MoveDelta As Float
Dim pressed As Boolean
Dim SoftKey As IME ' only required if using EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Label1.Initialize("") ' define some views to interact with
Button1.Initialize("Button1")
Button2.Initialize("Button2")
TxtEdit.Initialize("dummy") ' make sure the EditText gets an event listener
BottomPanel.Initialize("")
SoftKey.Initialize("") ' IME library used to force showing of soft keyboard for the EditText
Label1.Text = "Waiting for click or drag..." ' some user instructions
Button1.Text = "I am a Button1 CLICK ME"
Button2.Text = "I am a Button2 CLICK ME"
TxtEdit.Hint = "type something here"
Activity.AddView(BottomPanel,0,0,100%x,100%y) ' build the User Interface
BottomPanel.AddView(Label1,10,10,Activity.Width - 20,60)
BottomPanel.AddView(Button1,10,100,Activity.Width - 20,60)
BottomPanel.AddView(Button2,10,180,Activity.Width - 20,60)
BottomPanel.AddView(TxtEdit,10,260,Activity.Width - 20,60)
TransparentPanelOnTop.Initialize("TransparentPanelOnTop") ' now put a transparent panel on top of everything
Activity.AddView(TransparentPanelOnTop,0,0,100%x,100%y)
TransparentPanelOnTop.Color = Colors.Transparent
TransparentPanelOnTop.BringToFront
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub DeltaXY(x As Float, y As Float) As Float ' support routine for dragging
Return Abs(StartX - x) + Abs(StartY - y) ' does not need to be the actual distance moved
End Sub
Sub TransparentPanelOnTop_Touch (Action As Int, x As Float, y As Float)
'Note: always sends the touch action to TransparentPanelOnTop
'Note: x and y are not integers
Select Action
Case Activity.ACTION_DOWN ' everything starts with this action
SendAction(BottomPanel,x,y,"DOWN") ' PRESS a view eg highlight a button
StartX = x
StartY = y
LastX = x
LastY = y
MoveDelta = 0
pressed = True
Case Activity.ACTION_MOVE
If pressed Then ' check to see if we are really moving
MoveDelta = Max(MoveDelta, DeltaXY(x,y))
If MoveDelta > 1dip Then
SendAction(BottomPanel,StartX,StartY,"UP") ' un-press the view since we are now moving
pressed = False ' skip this test for subsequent moves
End If
End If
BottomPanel.Left = BottomPanel.Left + Round(x) - Round(LastX) ' DRAG the panel left or right
BottomPanel.Top = BottomPanel.Top + Round(y) - Round(LastY) ' DRAG the panel up or down
LastX = x ' these MUST be floating point values
LastY = y
Case Activity.ACTION_UP
SendAction(BottomPanel,x,y,"UP") ' make sure view not left in pressed state
pressed = False
If MoveDelta <= 1dip Then SendAction(BottomPanel,x,y,"CLICK") ' CLICK if we have not moved
End Select
End Sub
Sub SendAction(P As Panel, x As Int, y As Int, ActionToSend As String)
' Send a Press or Unpress or Click or Focus to a child view on a given panel
' uses reflection library (and IME library for the EditText)
' Note: x,y cordinates automatically converted to integers
Dim i, newx, newy As Int
newx = x - P.Left ' translate Top panel x,y coords to Bottom panel coords
newy = y - P.Top ' assumes the top panel does not move and positioned at 0,0
For i = P.NumberOfViews - 1 To 0 Step -1 ' start with last view added to the parent panel in case there are overlapping views
Dim v As View
v = P.GetView(i)
If (v.Left < newx) AND ((v.Left + v.Width) > newx) AND (v.top < newy) AND ((v.top + v.Height) > newy) Then
Dim r As Reflector
r.Target = v
Select ActionToSend
Case "DOWN"
r.runmethod2("setPressed","True","java.lang.boolean")
Case "UP"
r.runmethod2("setPressed","False","java.lang.boolean")
Case "CLICK"
If v Is EditText Then
r.runmethod2("setFocusableInTouchMode","True","java.lang.boolean") ' only required on some devices
' r.RunMethod("requestFocusFromTouch") ' SoftKey below will also request focus
SoftKey.ShowKeyboard(v) ' force the SoftKeyboard to show
Else
r.RunMethod("performClick")
End If
End Select
Exit ' exit the for loop - only apply the action to one view on the panel
End If
Next
End Sub
'These buttons and labels are on the BottomPanel underneath the TransparentPanelOnTop
Sub Button1_Click
Label1.Text = "You Clicked Button1 with positions : X = " & StartX & " Y = " & StartY
End Sub
Sub Button2_Click
Label1.Text = "You Clicked Button2 with positions : X = " & StartX & " Y = " & StartY
End Sub
I hope this will prove useful to both newcomers and existing forum users.
bluejay