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!
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
Check out OnLongClickListener: http://developer.android.com/reference/android/view/View.OnLongClickListener.html
Use an OnTouchListener or an SimpleOnGestureListener instead. Both of these listeners distinguish between when a user presses down on the screen and lifts up their finger.
Related
When I try to drag my finger from one button to other, the first is released, the second is not pressed.
Practically I need the second to be pressed. I thought maybe there was some specific Listener to do this, but I could not find it.
Also, I would need to perform certain operations at the exact time that the button is released (banally, the color of the button can be stored in a local variable or in a text view as shown in the sample figure).
Sample Image
Thanks for the suggestions!
Have a look at the OnTouchEvent-Method. You could override it and listen for Motion Events like ACTION_HOVER_ENTER and ACTION_HOVER_EXIT.
I don't know how exactly this type of buttons is called that's why I can't even google about it.
You use that button to unlock phone - tap on it and move from left to right.
I'd like to add same button in my app. How to do it?
You should use SeekBar to achieve what you want. http://developer.android.com/reference/android/widget/SeekBar.html
Here is a good example
http://www.techrepublic.com/blog/app-builder/androids-seekbar-your-way/943
Try this for a library
https://github.com/sitepoint-editors/SwipeButtonExample
and this for a DIY
https://rightclicksolutions.wordpress.com/2014/04/09/android-slide-to-unlock-like-ios-mb-slider-slider/
You can overide onTouch() and change the location of the button acording to the x position. and when you lift the finger you can overide onUp() (im not sure thats the name of the method) and create an animation that will lead the button back to its place.
From your question I get that you might think its a button you can simply add from a list of buttons, well, its not. you need to manualy create it.
which function is used for keeping track of all user interactions in Android?
I have used the onuserinteraction() function, but that does not include touching the mouse.
It includes clicking the mouse or any key event.
So is there any function which keeps track of touching the mouse also?
Take a look at these links:
View.OnTouchListener
Learn Android Tutorial 1.32 – Android’s OnTouchListener and MotionEvent
I hope this helps you...
I dont think android has a "mouse". U can only use onClick listeners.
I have a button that plays a sound clip when it is pressed. I would like the button to appear pressed during the duration of the sound clip. By that, I mean that I would like the button to take on the default pressed-button appearance while the sound is playing. How can I implement this? I have tried using a number of things in the onClickListener (such as setSelected, requestFocus, etc), but none of those do the trick. I have also tried changing the onClickListener to an onTouchListener, again with no dice. Am I wrong in assuming that there must be a way to simply set the button image to appear pressed? (BTW, the button object is of type Button, not ImageButton).
Thanks for any advice!
Please see this question. It details a couple of different ways this can be done.
Lookup 'selector' drawables and let android take care of this for you.
Hello i make appliction like Drum Studio. I need make support for touch more one button at once. For example: User touch button1(AND HOLD IT!) it play sound, but user still hold its, user click another button and it play sound too. How make it programmatically? By default onTouch or onClick use, if 1 button pressed, another buttons not react before button isn't released.
Use the onKeyDown() events to trigger each response and onKeyUp() events to finish them.
http://developer.android.com/reference/android/view/KeyEvent.html
http://developer.android.com/reference/android/view/KeyEvent.Callback.html
For touchs, you can use the OnTouchListener
http://developer.android.com/reference/android/view/View.OnTouchListener.html
and determine the kind of touch by the MotionEvent:
http://developer.android.com/reference/android/view/MotionEvent.html
To clarify my response: To make a sound while holding one button, you can start the sound when the ACTION_DOWN action in the MotionEvent event is triggered and stop it with the ACTION_UP action of the same event.
Create a class extending View where you're holding all your buttons.
Override the onTouchEvent vor that class.
Switch between the different actions.
Use event.getX() and event.getY() to figure out where the touch event was originated.
Check which button is located at that position
Play the sound accordingly.
You cannot touch two separate Views at the same time, even if multi-touch is supported on your device. If you touch Button1 first, all subsequent MotionEvents (including ones with separate pointer IDs) are bound to that View.
Unless you're using a dedicated game engine like AndEngine that has this sort of functionality built in, your only option is to capture all the MotionEvents yourself (most easily with an empty ImageView covering your entire screen) and route them to the appropriate Views based on their screen coordinates. This can get tricky, especially with multiple touch pointers, but the Android UI framework does not natively support the kind of multi-touch you require.
You definitely want to be using Android 2.0 or higher - previous versions don't support multitouch.
Check the pid in your onTouch - 0 is a single touch, 1 is the second.