problem with onclick android - android

i've been searching the web but i find no answer to my question. I have a button, when you press it, it will play a sound. The problem is that when you touch a button on the screen it goes to the onClickListener() only after the button have been released. I need it to run the listener when the button is pressed not when it's released, because this cause a delay when playing the sound. I tried onTouchListener() and it didn't work either, because the sound get's played every time i move the finger over the button. I tried onKeyDown() but it won't work for screen buttons. Any ideas? Some help will be appreciated.
Thanks

You can use OnTouchListener and test the event action:
public boolean onTouch (View v, MotionEvent event) {
if (event.getAction () == MotionEvent.ACTION_DOWN) { // ...
}
}

Maybe you can still use your implementation for onTouchListener(). Just set a boolean flag for when the user starts touching the button, then while it is set true (meaning the user hasn't released the button yet) do not play the sound. When the user releases the button (ACTION_UP), set the flag back to false. This would mean you are ready to play the sound again.

Related

Can you disable that a button is clicked on enter

So I am currently working on a scanner function and I need to read out a chain of key inputs.
The problem now is that whenever the scanner has completed scanning a code it presses Enter.
That in return triggers a click event on the currently focused item.
Is there any way to stop that from happening? So far I didn't find any information whatsoever on how to stop that from happening. Any help is much appreciated!
The easiest way would be to add a key listener that does nothing:
view.setOnKeyListener { v, keyCode, event ->
true
}
You return true meaning that you handled the event, while actually didn't do anything.

Gluon Home Button Events

I'm using Gluon and have an audio player. When I call the audio and play it all works normally until I press the home button. What I want it to do is to stop the music not to continue playing as it does now. Currently I've tried using an event listener to capture the event and stop the music but it's not recognising the event, I'm thinking either I have assigned the wrong keycode or it simply doesn't work like that. I have a setOnHiding method in the view already and that only works on the back button. I've also tried setOnHidden and setOnCloseRequest as well with no luck. The event listener is below.
if (event.getCode().equals(KeyCode.HOME) && KeyEvent.KEY_PRESSED == event.getEventType()) {
if (service1 != null) {
service1.backPressed();
}
Add a Listener to LifecycleEvent.Pause:
Services.get(LifecycleService.class).ifPresent(s -> s.addListener(LifecycleEvent.PAUSE, () -> stopPlayback());
The PAUSE event is fired when an application loses focus (e.g. on Android / iOS when the focus is switched out of view (but still running in the background)).

Send data to arduino as long as the button is being pressed

I want to send data to an arduino mega 2560 as long as a button is being pressed and when that button is released it will stop sending informations. I am using onTouchListener with MotionEvent constants. But when I run this on my phone I press the button and it sends data even though after a while I release it. Where am I being wrong here?
switch (v.getId()) {
case R.id.left1: // check what button is pressed
while(event.getAction() == MotionEvent.ACTION_DOWN) {
bt.sendData("1"); // while pressing the button it sends data
}
if(event.getAction() == MotionEvent.ACTION_UP) {
// when it stops, do nothing
}
break;
}
return true;
Your problem is in infinite loop while(event.getAction() == MotionEvent.ACTION_DOWN) that you start upon receiving the first event.
OnTouchListener is called for each event that is dispatched to view, down and up are separate events and event does not change while being processed.
So to solve your problem - you need to send data from a separate thread.
Start it on ACTION_DOWN and also have a flag that will be modified on ACTION_UP to indicate thread to exit.
You have to set the flag of bt.sendData to false when button is released which seems to be absent in your code.
It's like you open tap for water but forget to close the tap when you are finished. Hope it helps

Android Camera Button Half Way Pressed Listener

I am building a custom camera app and have got the basics to work. I have also been able to block the camera button from initializing the real camera app. The only thing that I would like to do is build in autofocus when the camera button is half pressed.
I am comfortable using camera.autofocus, but cannot find a way to listen for the camera button to be halfway pressed (like the default camera app does) to start the autofocus call.
Is there a keycode or another way to listen for the camera button being depressed to its half way point?
I got a little creative and just toasted any key down event in android. I ended up finding out that the key code for camera focus is 80 this way. This also matches up with the android documentation once I knew what I was looking for.
http://developer.android.com/reference/android/view/KeyEvent.html
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
Toast.makeText(this, new Integer(keyCode).toString(), Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Hope this helps others.
The camera button is a virtual button on the screen, I am not sure how it can the half pressed, or even could be mimicked in any way.

Using an Android Long-Button press to increment/decrement counter

I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.
First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.
Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.
Basically I am doing something like this:
btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
..code..
return true;
}
});
OnLongClick will only be called once per press. It isn't going to work for your purpose.
If I understood your question correct this can be achieved using a OnLongClickListener.
Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp
I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.

Categories

Resources