Keep on doing an action while button is pressed after long click - android

Sorry if the title makes no sense. What I'm trying to accomplish is to be able to long click a button, and once the long click is detected, to keep on doing a certain task.
To put it into context, I have a view with a background that changes to a random color on click. I would like to have the background keep on changing color as long as I long click the button. So, basically, upon long click and keeping the button pressed, the background will keep on changing.
Thank you very much folks :)

This isn't possible with view.setOnLongClickListener() because an OnLongClickListener has a very specific way of functioning. As soon as enough time has passed, the listener will activate and the code is run.
What you want is to use view.setOnTouchListener(). This way you can set a Timer when it detects a MotionEvent.ACTION_DOWN. Once the timer is long enough to equal a long click, you can have it trigger your color changing code repeatedly until the OnTouchListener detects a MotionEvent.ACTION_UP. That's when you can stop the color changing code.

Related

Android Button Click not go through, but action still take place

I want when a button is pressed in my activity, for the click not to occur (such as clickEnabled being false) but I want the action that would have occurred to still happen.
For example, say I have a Dialer application:
When button "One" is pressed, the user will not see that the button was actually clicked, but the action of adding a 1 to the edittext will still occur. Thank you for your help!
I think you can make a selector as your button's background.and most important is samecolor no matter that is click , touch or idle . So through this ,you fake a no click effect.
And same time ,you can also add a click listener to this button to get the click event.
Hope that give you some suggestion.
I think you should simply use onTouchListener event for buttons instead of onclickListener

Android Method to Enable/Disable Touch Screen

Hi there is thery any way to invoke a method that enables or disables the Touch Screen of Android?
I want this, because I have several buttons on my activity. When the user clicks on a button, it takes a few seconds to start the following activity, and because of that, while waiting that time, I don't want the user to be able to press anything.
I used a boolean that is True at start, then It changes to false when I click on the first button. And to every click on a button I check if the boolean is true...
But the problem is that Visually the user can click the button, it gets that look of being pressed..
So is there any good method that disables the entire touch screen ? And another that enables the entire touch screen ?
Thanks alot in advance ;)
But the problem is that Visually the user can click the button, it gets that look of being pressed
Disable the buttons, using setEnabled(false). This will not only prevent the user from clicking on them, but they will visually appear disabled, to let the user know that the user cannot click on them. It is important for the user to get the proper visual feedback about the buttons being disabled (and later enabled).
is thery any way to invoke a method that enables or disables the Touch Screen of Android?
Not really.

Pressing multiple buttons at the same time

I'm working on an app which presents users with four buttons and a timer. The users then tap as fast as they can, each user on his own button, and the one with the most taps at the end wins.
I can't use onClick here, because it locks up the UI thread until the button is released, effectively blocking other button presses. I have searched around for a bit and found that I could use onTouch, but it doesn't work the way I want it to. If a user has touched and is still holding a button, any subsequent touches on other buttons will behave as though this first button was pressed.
Someone suggested the use of an image, transparent and stretched across the entire visible UI. One could then read any touches on this imageview and assign a touch to a particular button based on the coordinates of the press. (I guess I would take a screenshot of my UI with the four buttons visible, open it in paint and write down the coordinates of the borders for each button, then use those coordinates in the code to figure out which button the user was trying to press.)
Can anyone help me with this method of work, give me a quick example, or link to where I can learn how to do it and implement it in my app? It would be much appreciated.
TL;DR: How to use an image, transparent and stretched across the entire visible UI and read the coordinates of each press (even if multiple at once)?

How to distinguish between click and double click in an activity?

In my activity I would like to vibrate if I click once on a button, and do something else (for example closing the window with the button "close") if I click twice on it. I'm thinking of measuring the time between 2 clicks on a button and if it's less than a given intervall, then doing the given function, but I don't know how can I do that, and it may be too complicated.
I implemented the OnFocusChangeListener of the button after setting Focusable to false, and it can vibrate, but I don't have the solution of double click.
Try to use onDoubleTapListener.
Here are examples:
http://android-journey.blogspot.com/2010/01/android-gestures.html

Android Button performClick

In order to fudge multitouch buttons, I set a massive invisible imageView over top of everything. The I just poll for where it was touched and call performClick on the button in that area under it. Now my problem is that performClick only calls the onClick method, and doesn't actually perform a legitimate button press, so there's no animation (color change etc). I have a custom xml for the buttons, and it worked fine without the imageView. I try using setPressed and setEnabled, but the png never changes, and the button looks static. What am I missing?
How about tying a Boolean to each button. All the button logic does is flip flop the variable and maybe change an indicator (button color or text) to pushed/not pushed. Shouldn't be more than 3 or 4 lines of code executed per button press. Fire the heavy code when a 'do it' button is pressed. That would have to be faster than computing which button was pressed under the overlay...

Categories

Resources