Add Touch to a button

stevel05

Expert
Licensed User
Longtime User
In response to this question.

you can add touch to a button using reflection:

B4X:
Dim R As Reflector
R.Target=Button1
R.SetOnTouchListener("Button1_Touch")

.......

Sub Button1_Touch(ViewTag As Object, Action As Int, X As Float,Y As Float, Motionevent As Object) As Boolean
   Log("Touched")
End Sub

But it still wont trigger unless the finger is touched on the object that is being monitored.

You could use my Hotspot code module which would monitor a whole panel and tell you when a finger moves into the hotspot.

Download from here.

Change the code in Panel1_Touch from Activity.ACTION_DOWN to Activity.ACTION_MOVE.

Try it out, hopefully it'll give you what you want.
 
Last edited:

laviniut

Active Member
Licensed User
Longtime User
Sorry because i posted in wrong forum. and is a good idea you posted but i still need a doubleclick method.
so i repeat my question for customview: (or have anyone another idea to combine touch method and doubleclick method for a button ?)
I must create for my PhD an application for blind users. so i created an interface with buttons as customview. the blind user put a finger on screen and with TTS i can speak what button is. when he get wanted button he can doubleclick on it to start proper activity. till now all is fine.
but if the blind user is moving the finger on the screen without lifting the finger, when he get another customview button the method singleclick is not fired to use TTS, only if the finger is lift and put again on the screen.
Erel, can you create another method for customview like touch for a button (in addition to singleclick and doubleclick) ?
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
I saw you use a touch method for panel in CustomView Class. but the result is not what i want. i have a few customview buttons. when i put the finger on the screen, for the first button is rised singleclick (ok), but if i do not take my finger off the screen but moving it to next button, singleclick for next button is not rised. you know what i mean ?
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
customview buttons looks different. why ?

and another thing is that same code with customviews looks different on emulator and my galaxy s2, with same android 4.1.2. why is that ?
 

Attachments

  • screenshot_customview_emulator.png
    screenshot_customview_emulator.png
    17.2 KB · Views: 357
  • screenshot_customview_galaxyS2.png
    screenshot_customview_galaxyS2.png
    24.3 KB · Views: 281
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could have a look at chapter 5.3.3 Button with tooltip and additional buttons in the User's Guide.
There I use 4 buttons, they are in fact Panels (the source code is included).
A 'main' button and three additional ones.
In the Touch event of the main button with:
Action_DOWN a ToolTip is displayed, the background image is changed and some other tests are done.
Action_MOVE checks if the cursor is insides the area of a button, changes the bachgound images if necessary.
Action_UP if the cursor is insides the area of a button executes the function of that button, if the cursor is not insides of any button area nothing happens.

You could take this example as an inspiration for your specific needs.

Best regards.
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
You could have a look at chapter 5.3.3 Button with tooltip and additional buttons in the User's Guide.
There I use 4 buttons, they are in fact Panels (the source code is included).
A 'main' button and three additional ones.
In the Touch event of the main button with:
Action_DOWN a ToolTip is displayed, the background image is changed and some other tests are done.
Action_MOVE checks if the cursor is insides the area of a button, changes the bachgound images if necessary.
Action_UP if the cursor is insides the area of a button executes the function of that button, if the cursor is not insides of any button area nothing happens.

You could take this example as an inspiration for your specific needs.

Best regards.

Hi Klaus,
I just skimmed through the chapter quickly and have a brief questions. Is the Action capturing ONLY available at the Activity level (i see that buttons also have down and up events) and, If actions can be captured for buttond, can they also be captured for other viewsssuch as labels and images (my interest at this stage is for displaying tooltips)? Oh, yesterday, also is the ToolTips sub part of the core or do I need another library? Thanks.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It is possible to add a Touch event to any View with the reflection library.

The 'buttons' used in chapter 5.3.3 in the User's Guide are not Buttons but Panels using the Touch event.

The Tooltip routine is a routine that shows or hides a label and sets its text.
No library needed.

You could also have a look at this thread.

Best regards.
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
It is possible to add a Touch event to any View with the reflection library.

The 'buttons' used in chapter 5.3.3 in the User's Guide are not Buttons but Panels using the Touch event.

The Tooltip routine is a routine that shows or hides a label and sets its text.
No library needed.

You could also have a look at this thread.

Best regards.

Is the Tooltip routine part of the Core or do we have to create it? Thanks.
 
Upvote 0
Top