On pressed button block the other buttons - android

How could I make my button to do the on click action even if the button is pressed and the finger is still on the screen (the button is not released). Or I would accept the solution that when a button is pressed and is not release, the other buttons on the layout to not be blocked.

Instead of using an OnClickListener consider using an OnTouchListener. You can then detect when the user's finger touches:ACTION_DOWN and releases ACTION_UP the button.
A method like this would probably work fine:
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//DISABLE THE OTHER BUTTONS
break;
case MotionEvent.ACTION_UP:
// RE-ENABLE THE OTHER BUTTONS
break;
}
return true;
}

One option would be to use the onmousedown event rather than the onclick event in your javascript:
myButton.onmousedown = myOnClickFunction;

Related

Select other buttons on touch move

I'm writing a test which options are placed in buttons. I have a selector for buttons that displays normal and pressed state and everything works great.
What i want is that when user touches an option and moves to next option, the next button get focus and select. And when user takes the hand, perform the selected button click.
In images above you can see exactly what i mean.
You need to write a custom touch event management that will trigger setPressed(true) on MotionEvent.ACTION_DOWN OR MotionEvent.ACTION_MOVE (the default implementation expects to always receive MotionEvent.ACTION_DOWN).
Code like this should work (add it to all your buttons):
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (!v.isPressed()) {
v.setPressed(true);
}
return true;
case MotionEvent.ACTION_UP:
// perform action ON CLICK
break;
}
v.setPressed(false);
return false;
}
});

Android - OnTouchListener not firing if already pressing screen

I have a RelativeLayout with a single ImageView inside of it. The ImageView has an OnTouchListener attached to it which is working perfectly fine if I just press the ImageView. The problem I'm having though is that if I press and hold in the empty space outside of the ImageView and then try to press the ImageView the touch listener doesn't fire. It seems like the layout is absorbing the touch events somehow. Any ideas on how I could fix this?
The action defined by you here is a Multi-touch gesture. Go through this doc to know how to handle this type of actions.
Also take a look at the ACTION_POINTER_DOWN action define in MotionEvent. This is the event that will get called when additional fingers come down.
you mout use this code:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//your code
break;
case MotionEvent.ACTION_UP:
//your code
break;
case MotionEvent.ACTION_MOVE:
//your code
break;
default:
//your code
break;
}
return true;
}
});

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.
boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}

Android onTouch Listener Background Color Change

I'm using onTouchListener to change the background color when the user's finger is down and change back to default when the user's finger is up. The problem is that when the user's finger is down the background color changes but if the user doesn't takes the finger up and starts scrolling the background color doesn't change to default. Please help.
Below is my code:
V.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
V.setBackgroundColor(Color.BLUE);
break;
case MotionEvent.ACTION_UP:
V.setBackgroundColor(Color.BLACK);
break;
}
return true;
}
});
I've also tried to return false but the result is the same
There are a few different approaches you can use. You can look at MotionEvent.ACTION_MOVE and act when you receive that in your onTouch. You can look at MotionEvent.ACTION_OUTSIDE
and see if they have left the region your are checking.
You can also put a listener on your scroll View and change the background when it moves. Synchronise ScrollView scroll positions - android
Alternately, you could use a gestureListener and detect that.

Dragging and pressing buttons - android

I have several buttons and I would like to press on one of them and drag through another presing them. Could you tell me which MotionEvent or another functionality should I use. I'm using onTouchListener.
There is an image where you can see what I want to do (first ACTION_DOWN on 1st button and drag through 2nd-7th buttons still pressing the screen) and finally press every white buttons:
Below is my onTouch button code:
button1 = (Button) findViewById(R.id.button1);
button1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
soundIDs[0] = sound.play(R.raw.sample1);
button1.setBackgroundResource(R.drawable.white_clicked);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
sound.stop(soundIDs[0]);
button1.setBackgroundResource(R.drawable.white);
break;
}
return false;
}
});
You are setting the OnTouchListener on just the one button. That's not going to help you know when the pointer moves (e.g. user drags his finger) into another button.
You could set an OnTouchListener on the view that contains the buttons. Then check for ACTION_DOWN, ACTION_MOVE, and ACTION_UP events. You would then have to do some simple hit detection to figure out which button to activate.
Something along the lines of:
getWindow().getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
int action = event.getAction();
if (action != MotionEvent.ACTION_DOWN
&& action != MotionEvent.ACTION_MOVE
&& action != MotionEvent.ACTION_UP) return false;
Rect hitRect = new Rect();
Button button;
for(int i = 0; i < myButtons.size(); i++) {
button = myButtons.get(i);
button.getHitRect(hitRect);
if (hitRect.contains((int)event.getX(), (int)event.getY())) {
button.setText("hit!");
}
}
return true;
}
});
Where myButtons is an ArrayList of your buttons (piano keys in your example).
Also, you'd probably want to modify this to properly deactivate the currently active button if the user's touch leaves the button, but doesn't hit another button.
I tested the above code on an android device with a layout that has 3 buttons in a row. Dragging your finger across all the buttons causes each button's text to change to "hit!"
Like I said above, you were setting the touch listener on the just one button, that will not work. In this example I have set the touch listener on the entire view for the activity.

Categories

Resources