how to increment a value continuosly when a button is pressed? - java

I have a button and a textview with the number 1 displayed when my app is running. When i press and hold the button i want the value to continuosly increment till I let go of the button. It is very frustrating for me because i used to know how to do this and it was as simple as setting the button properties to something in xml. However I have forgotten how and i have searched through the internet and the examples i have found implement an ontouch listener that only increments the value by one. But i want the value to be increment continuosly depending on how long the user presses the button. this is not a repeat of
Triggering event continuously when Button is pressed down in Android
Triggering event continuously when Button is pressed down in Android
as they did not work for me. so can some remind me how to get a button to increment a value continuosly when you press and hold the button?

Use onTouchListener and start/stop your counter routine on MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP

Android long-touch event
bramburys answer in the link above worked perfectly whilst other answers incremented one at a time when the button was pressed whilst bramburys incremented continuously.

Related

How to override onBackPressed but also get the touch event?

I'm working on an Android project in Java and I'd like to get the MotionEvent when the user hits the back button (onBackPressed).
I know how to override the back button press, and i know how to get the touch event from touches in my activity, but I couldn't find a way to get the touch event details out of the back button press.
The goal is to write the user's touch event details (as if the user touches a regular view) to a file, even when the back button is pressed.
Is it possible? if so, how can I do it?

Set a timer for the visibility of a button

This is purely theoretical put let's say once I put a timer for a button to appear only 84600 second later (like 24h), will it the count down continue after the app is shut down ?
Can the user close the app and then come back the next day and find the button after the countdown is done ?
Or is there a better way to make it happen ? Like to allow the user to see it only after 24h ?
you can use ScheduledExecutorService to create such a timer. Then on every tick, you can open the app if you want, and show the button, or just show the button if the user open the app.
documentation is available here.

What happens when a user holds the headset button

I'm trying to find out what happens when a user pushes the media button of headset. I know the phone recognizes holding the button, but when I test it myself and hold the button broadcast will send a ACTION_MEDIA_BUTTON. When I push the button down and release it, I get two broadcasts (one for ACTION_DOWN and one for ACTION_UP). But when I hold it doesn't send anything.
This is how most buttons work for anything. Almost no hardware will tell you when something is being pushed down, just when a change is made.
If you want to know how long a button is held down for, you want to start recording the time using something like System.currentTimeMillis when you receive an ACTION_DOWN message and when you receive an ACTION_UP you can record the end time and then calculate the difference.

Update TextView numerous times with Buttons

I am making a simple shopping cart application where the user can select items (via buttons) and their running total will be displayed in a text view.
I am fine with having the text view being updated on a single click but I am struggling to figure out how to write my code if the same button is pressed more than once. For example a button writes a value of £3 into the TextView, if this button was clicked again I want the TextView to increase to £6 and so on.
Further to this I want to be able to have more than value added to the TextView from different buttons. I imagine this is more of a Java question as opposed to Android but seeing as I'm a bit of a newbie to both all advice is welcome!
Into your class if you have price value add an incerementPriceValue and make get/set functions for the last one.
If user press the button
setIncrementValue(getIncrementValue()+priceValue);
setText(getIncrementValue+simbolstring);
Create a int variable count and on the click increase the count value and set that value on the textView

How to make a protection of many times pressing the button?

In my app I use a camera, and I want to take a pictures. In my app is button (Photo). If I press it one times - all work perfect, but if I press button many times until camera take picture, my app hangs. How can I fix it?
In your onClickListener call Button.setEnabled() and set it to false.
Then set it to true when you've finished taking the photo.
Use a listener that disables the button on press (setEnalbed(false)), than start a countdown thread that re-enables it after some time, 200ms maybe, or whatever fits the best.
After second thoughts this might not be a really good idea.
There is a chance that the thread will not be scheduled to run, so if you exactly know the point when you can re-enable the button in your code, don't use threads.

Categories