Android Question Add a button to TouchImageView

grafsoft

Well-Known Member
Licensed User
Longtime User
Hi,
I want to add a small button just at the position the user has touched.
How is this done?
I try

B4X:
Sub TouchImageView1_Click(X As Int, Y As Int)
   Log("Click X="&X&", Y="&Y)
   Dim b As Button
   b.initialize ("touchbtn")
   bcount=bcount+1
   b.Tag=bcount
   b.Text=bcount
   b.Background=Colors.red
   Activity.AddView (b,X,Y,10,10)
end sub

I know this is wrong, but I cannot add a view to the touchimageview.

Thanks

Peter
 

grafsoft

Well-Known Member
Licensed User
Longtime User
In found it out myself, but now I have another problem
B4X:
Sub Globals
   Dim TouchImageView1 As TouchImageView
   Dim pnl As Panel   

End Sub

Sub Activity_Create(FirstTime As Boolean)
   phon.setscreenorientation (0)
   
   TouchImageView1.Initialize("TouchImageView1")
   Activity.AddView(TouchImageView1, 0, 0, 100%x, 100%y)
   
   TouchImageView1.Gravity=Gravity.FILL
   
   Dim Bitmap1 As Bitmap
   Bitmap1.Initialize(Main.outdir, Main.thepic)
   TouchImageView1.SetBitmap(Bitmap1)
   
   If FirstTime Then
     '   Rect parameters are: left, top, right, bottom
     SourceImageRect.Initialize(0, 0, Bitmap1.Width, Bitmap1.Height)
     TouchImageViewRect.Initialize(0, 0, TouchImageView1.Width, TouchImageView1.Height)
     
   End If
   
   ' put an invisible panel over the touchimageview
   pnl.Initialize ("pnl")
   Activity.AddView(pnl, 0, 0, 100%x, 100%y)
   pnl.Color = Colors.ARGB(0, 0, 0, 0)
  

End Sub


Sub TouchImageView1_Click(X As Int, y As Int)
   Log("Click X="&X&", Y="&y)
   
   Dim b As Button
   b.initialize ("touchbtn")
   bcount=bcount+1
   b.Tag=bcount
   b.Text=bcount
   b.color=Colors.red
   ' Now put the button on the panel
   pnl.addview (b,x,y,40,40)
   

End Sub

When I touch some position in the upper left corner, the button appears much farther to the right and down. I want it exactly where I touched the screen.

What am I doing wrong?

Thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…