Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener Hold button?

I am currently working on my final project for an intro android app development class. My final project was going to be a music player where you could click a button and it would play a certain note on a certain instrument. One thing I could not figure out is a way to have a sound file repeatedly play as a user holds a button. I understand how the OnClickListener works, but is there one for holding a button that I am missing? Thanks so much!

like image 955
swspoerle Avatar asked Apr 04 '13 16:04

swspoerle


People also ask

What is the difference between onClick and OnClickListener?

View. OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.

How do I use OnClickListener?

Using an OnClickListener You can also declare the click event handler programmatically rather than in an XML layout. This event handler code is mostly preferred because it can be used in both Activities and Fragments. There are two ways to do this event handler programmatically : Implementing View.

What is long click in Android?

A Long Press refers to pressing a physical button or tap a virtual button on a touchscreen and holding it down for a second or two. Employed on touchscreens, smartphones, tablets, and smartwatches, the long press or long tap increases the user interface's flexibility.


2 Answers

You need to use touch listeners, not click listeners, because the latter, as name indicates is for click and click is is one time limited event, while touch is not.

See onTouch() and in general docs: http://developer.android.com/guide/topics/ui/ui-events.html

like image 144
Marcin Orlowski Avatar answered Sep 26 '22 00:09

Marcin Orlowski


Check out OnLongClickListener: http://developer.android.com/reference/android/view/View.OnLongClickListener.html

like image 39
Lorenzo Milani Avatar answered Sep 23 '22 00:09

Lorenzo Milani