Android Question DraggableView Class help

rfresh

Well-Known Member
Licensed User
I'm trying to get the DraggableView example to work.

I'm getting an error: unknown type draggableview

As the attachment shows, I have the Reflection Lib checked.

B4X:
#Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName: 
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Main activity module
Sub process_globals

End Sub

Sub Globals
    Dim Button1 As Button
    Dim Button2 As Button
    Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    Dim dv1, dv2, dv3 As DraggableView '<---- error on this line
    dv1.Initialize(Activity, Button1)
    dv2.Initialize(Activity, Button2)
    dv3.Initialize(Activity, EditText1)
End Sub

'***************************
'DraggableView class module
Sub Class_Globals
    Private innerView As View
    Private panel1 As Panel
    Private downx, downy As Int
    Private ACTION_DOWN, ACTION_MOVE, ACTION_UP As Int
End Sub

Sub Initialize(Activity As Activity, v As View)
    innerView = v
    panel1.Initialize("")
    panel1.Color = Colors.Transparent
    Activity.AddView(panel1, v.Left, v.Top, v.Width, v.Height)
    ACTION_DOWN = Activity.ACTION_DOWN
    ACTION_MOVE = Activity.ACTION_MOVE
    ACTION_UP = Activity.ACTION_UP
    Dim r As Reflector
    r.Target = panel1
    r.SetOnTouchListener("Panel1_Touch") 'why reflection instead of the regular Panel_Touch event? Good question which deserves a forum thread of its own (not related to classes)...
End Sub

Private Sub Panel1_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
    If ACTION = ACTION_DOWN Then
        downx = x
        downy = y
    Else
        innerView.Left = innerView.Left + x - downx
        innerView.Top = innerView.Top + y - downy
        panel1.Left = innerView.Left
        panel1.Top = innerView.Top
    End If
    Return True
End Sub
 

Attachments

LucaMs

Expert
Licensed User
DraggableView is a class. You must create a class named DraggleView and move all the code you have after the asterisks to this class (empty the new class before)
 

rfresh

Well-Known Member
Licensed User
I've looked around here and on Youtube but I cannot find any examples of how that is done. There is a lot of discussions about classes and Erel has a tutorial but he doesn't talk about how you actually setup your files and class structure. He talks about modules and classes but gives no full examples that I can find.

Do I create a separate file? If yes, how do I reference it in my main file?

Thank you...
 

LucaMs

Expert
Licensed User
Simply the code you found was not posted in separated blocks!

This one is the Main (activity module):
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

'Main activity module
Sub process_globals

End Sub

Sub Globals
Dim Button1 As Button
Dim Button2 As Button
Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
Dim dv1, dv2, dv3 As DraggableView '<---- error on this line
dv1.Initialize(Activity, Button1)
dv2.Initialize(
Activity, Button2)
dv3.Initialize(
Activity, EditText1)
End Sub

This one is a class:
'***************************
'DraggableView class module

Sub Class_Globals
Private innerView As View
Private panel1 As Panel
Private downx, downy As Int
Private ACTION_DOWN, ACTION_MOVE, ACTION_UP As Int
End Sub

Sub Initialize(Activity As Activity, v As View)
innerView = v
panel1.Initialize(
"")
panel1.Color =
Colors.Transparent
Activity.AddView(panel1, v.Left, v.Top, v.Width, v.Height)
ACTION_DOWN =
Activity.ACTION_DOWN
ACTION_MOVE =
Activity.ACTION_MOVE
ACTION_UP =
Activity.ACTION_UP
Dim r As Reflector
r.Target = panel1
r.SetOnTouchListener(
"Panel1_Touch") 'why reflection instead of the regular Panel_Touch event? Good question which deserves a forum thread of its own (not related to classes)...
End Sub

Private Sub Panel1_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
If ACTION = ACTION_DOWN Then
downx = x
downy = y
Else
innerView.Left = innerView.Left + x - downx
innerView.Top = innerView.Top + y - downy
panel1.Left = innerView.Left
panel1.Top = innerView.Top
End If
Return True
End Sub

Create a new class:
upload_2018-4-24_5-32-12.png



Name it: "DraggableView".

Delete all its (default) code.

Cut (from your project) the class code and paste it to the new class.
 

rfresh

Well-Known Member
Licensed User
Unless I'm mistaken, Button1 and Button2 should be movable like the TextEdit1 object is, but I can't move either button with my finger on my phone screen. I can touch EditText1 and move it around the screen but not the buttons.
 

Attachments

LucaMs

Expert
Licensed User
Unless I'm mistaken, Button1 and Button2 should be movable like the TextEdit1 object is, but I can't move either button with my finger on my phone screen. I can touch EditText1 and move it around the screen but not the buttons.
Tested your project:
I can move all the three views (how do you distinguish them? :) - write something).

Anyway, try to move:

Dim dv1, dv2, dv3 As DraggableView

to the Sub Globals.
 
Last edited:

rfresh

Well-Known Member
Licensed User
Hmmm...I did those two things but still no go. I can't move the buttons. I'm using the B4A IDE if that makes any difference.
 

Attachments

rfresh

Well-Known Member
Licensed User
I'm testing on my phone an LG V20 and I'm testing on the 6 inch screen emulator.

Yes, I see it's working fine for you. Well, thanks for the help...I'll keep playing around with it and see if I can figure out how to get it working.
 

LucaMs

Expert
Licensed User
Tried and I see different things! (buttons over panels).

Have to check the source code...

most likely it is due to the elevation of panels.
 
Top