How to use View.OnTouchListener instead of onClick

AndroidAndroid LayoutUser Input

Android Problem Overview


I'm developing an Android 2.2.2 application for a client and he wants to do the following:

Now I have a button with an onClick event but he doesn't like, he wants to dectect when user release the button.

I've found View.OnTouchListener which I think this is what I need to use but, is there any posibility to add this event to xml like I did with onClick?

<ImageButton
    android:id="@+id/btnSaveNewGate"
    android:layout_width="@dimen/btnSaveNewGate_width"
    android:layout_height="@dimen/btnSaveNewGate_height"
    android:layout_below="@+id/radioGrGateType"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/btnSaveNewGate_marginTop"
    android:background="@null"
    android:contentDescription="@string/layout_empty"
    android:onClick="onSaveNewGateClick"
    android:scaleType="fitXY"
    android:src="@drawable/save_gate_selector" />

I have two questions more:

Which is the event associated when user releases his finger?

Is there any guidelines which prohibit using View.OnTouchListener instead of onClick?

Android Solutions


Solution 1 - Android

The event when user releases his finger is MotionEvent.ACTION_UP. I'm not aware if there are any guidelines which prohibit using View.OnTouchListener instead of onClick(), most probably it depends of situation.

Here's a sample code:

imageButton.setOnTouchListener(new OnTouchListener() {
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
		
			// Do what you want
			return true;
		}
		return false;
	}
});

Solution 2 - Android

Presumably, if one wants to use an OnTouchListener rather than an OnClickListener, then the extra functionality of the OnTouchListener is needed. This is a supplemental answer to show more detail of how an OnTouchListener can be used.

Define the listener

Put this somewhere in your activity or fragment.

private View.OnTouchListener handleTouch = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("TAG", "touched down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("TAG", "moving: (" + x + ", " + y + ")");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("TAG", "touched up");
                break;
        }

        return true;
    }
};

Set the listener

Set the listener in onCreate (for an Activity) or onCreateView (for a Fragment).

myView.setOnTouchListener(handleTouch);

Notes

  • getX and getY give you the coordinates relative to the view (that is, the top left corner of the view). They will be negative when moving above or to the left of your view. Use getRawX and getRawY if you want the absolute screen coordinates.
  • You can use the x and y values to determine things like swipe direction.

Solution 3 - Android

OnClick is triggered when the user releases the button. But if you still want to use the TouchListener you need to add it in code. It's just:

myView.setOnTouchListener(new View.OnTouchListener()
{
    // Implementation;
});

Solution 4 - Android

for use sample touch listener just you need this code

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    ClipData data = ClipData.newPlainText("", "");
    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    view.startDrag(data, shadowBuilder, null, 0);

    return true;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVansFannelView Question on Stackoverflow
Solution 1 - AndroidAndy ResView Answer on Stackoverflow
Solution 2 - AndroidSuragchView Answer on Stackoverflow
Solution 3 - AndroidCaseyBView Answer on Stackoverflow
Solution 4 - AndroidRahimpoorDeveloperView Answer on Stackoverflow