Android APIs
public class

TouchUtils

extends Object
java.lang.Object
   ↳ android.test.TouchUtils

Class Overview

Reusable methods for generating touch events. These methods can be used with InstrumentationTestCase or ActivityInstrumentationTestCase2 to simulate user interaction with the application through a touch screen.

Summary

Public Constructors
TouchUtils()
Public Methods
static void clickView(InstrumentationTestCase test, View v)
Simulate touching the center of a view and releasing.
static void drag(ActivityInstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void drag(InstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount)
Simulate touching a specific location and dragging to a new location.
static void dragQuarterScreenDown(InstrumentationTestCase test, Activity activity)
Simulate touching in the center of the screen and dragging one quarter of the way down
static void dragQuarterScreenDown(ActivityInstrumentationTestCase test)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void dragQuarterScreenUp(ActivityInstrumentationTestCase test)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void dragQuarterScreenUp(InstrumentationTestCase test, Activity activity)
Simulate touching in the center of the screen and dragging one quarter of the way up
static int dragViewBy(ActivityInstrumentationTestCase test, View v, int gravity, int deltaX, int deltaY)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewBy(InstrumentationTestCase test, View v, int gravity, int deltaX, int deltaY)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewTo(ActivityInstrumentationTestCase test, View v, int gravity, int toX, int toY)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewTo(InstrumentationTestCase test, View v, int gravity, int toX, int toY)
Simulate touching a view and dragging it to a specified location.
static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v, int stepCount)
Simulate touching the center of a view and dragging to the bottom of the screen.
static void dragViewToBottom(ActivityInstrumentationTestCase test, View v, int stepCount)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void dragViewToBottom(InstrumentationTestCase test, Activity activity, View v)
Simulate touching the center of a view and dragging to the bottom of the screen.
static void dragViewToBottom(ActivityInstrumentationTestCase test, View v)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void dragViewToTop(InstrumentationTestCase test, View v, int stepCount)
Simulate touching the center of a view and dragging to the top of the screen.
static void dragViewToTop(ActivityInstrumentationTestCase test, View v, int stepCount)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void dragViewToTop(InstrumentationTestCase test, View v)
Simulate touching the center of a view and dragging to the top of the screen.
static void dragViewToTop(ActivityInstrumentationTestCase test, View v)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewToX(InstrumentationTestCase test, View v, int gravity, int toX)
Simulate touching a view and dragging it to a specified location.
static int dragViewToX(ActivityInstrumentationTestCase test, View v, int gravity, int toX)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewToY(ActivityInstrumentationTestCase test, View v, int gravity, int toY)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static int dragViewToY(InstrumentationTestCase test, View v, int gravity, int toY)
Simulate touching a view and dragging it to a specified location.
static void longClickView(InstrumentationTestCase test, View v)
Simulate touching the center of a view, holding until it is a long press, and then releasing.
static void longClickView(ActivityInstrumentationTestCase test, View v)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void scrollToBottom(InstrumentationTestCase test, Activity activity, ViewGroup v)
Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(InstrumentationTestCase, Activity)
static void scrollToBottom(ActivityInstrumentationTestCase test, ViewGroup v)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void scrollToTop(InstrumentationTestCase test, Activity activity, ViewGroup v)
Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(InstrumentationTestCase, Activity)
static void scrollToTop(ActivityInstrumentationTestCase test, ViewGroup v)
This method was deprecated in API level 3. ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test
static void tapView(InstrumentationTestCase test, View v)
Simulate touching the center of a view and releasing quickly (before the tap timeout).
static void touchAndCancelView(InstrumentationTestCase test, View v)
Simulate touching the center of a view and cancelling (so no onClick should fire, etc).
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public TouchUtils ()

Added in API level 1

Public Methods

public static void clickView (InstrumentationTestCase test, View v)

Added in API level 1

Simulate touching the center of a view and releasing.

Parameters
test The test case that is being run
v The view that should be clicked

public static void drag (ActivityInstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a specific location and dragging to a new location.

Parameters
test The test case that is being run
fromX X coordinate of the initial touch, in screen coordinates
toX Xcoordinate of the drag destination, in screen coordinates
fromY X coordinate of the initial touch, in screen coordinates
toY Y coordinate of the drag destination, in screen coordinates
stepCount How many move steps to include in the drag

public static void drag (InstrumentationTestCase test, float fromX, float toX, float fromY, float toY, int stepCount)

Added in API level 3

Simulate touching a specific location and dragging to a new location.

Parameters
test The test case that is being run
fromX X coordinate of the initial touch, in screen coordinates
toX Xcoordinate of the drag destination, in screen coordinates
fromY X coordinate of the initial touch, in screen coordinates
toY Y coordinate of the drag destination, in screen coordinates
stepCount How many move steps to include in the drag

public static void dragQuarterScreenDown (InstrumentationTestCase test, Activity activity)

Added in API level 3

Simulate touching in the center of the screen and dragging one quarter of the way down

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case

public static void dragQuarterScreenDown (ActivityInstrumentationTestCase test)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching in the center of the screen and dragging one quarter of the way down

Parameters
test The test case that is being run

public static void dragQuarterScreenUp (ActivityInstrumentationTestCase test)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching in the center of the screen and dragging one quarter of the way up

Parameters
test The test case that is being run

public static void dragQuarterScreenUp (InstrumentationTestCase test, Activity activity)

Added in API level 3

Simulate touching in the center of the screen and dragging one quarter of the way up

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case

public static int dragViewBy (ActivityInstrumentationTestCase test, View v, int gravity, int deltaX, int deltaY)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it by the specified amount.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
deltaX Amount to drag horizontally in pixels
deltaY Amount to drag vertically in pixels
Returns
  • distance in pixels covered by the drag

public static int dragViewBy (InstrumentationTestCase test, View v, int gravity, int deltaX, int deltaY)

Added in API level 3

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it by the specified amount.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
deltaX Amount to drag horizontally in pixels
deltaY Amount to drag vertically in pixels
Returns
  • distance in pixels covered by the drag

public static int dragViewTo (ActivityInstrumentationTestCase test, View v, int gravity, int toX, int toY)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toX Final location of the view after dragging
toY Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static int dragViewTo (InstrumentationTestCase test, View v, int gravity, int toX, int toY)

Added in API level 3

Simulate touching a view and dragging it to a specified location.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toX Final location of the view after dragging
toY Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static void dragViewToBottom (InstrumentationTestCase test, Activity activity, View v, int stepCount)

Added in API level 3

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case
v The view that should be dragged
stepCount How many move steps to include in the drag

public static void dragViewToBottom (ActivityInstrumentationTestCase test, View v, int stepCount)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test The test case that is being run
v The view that should be dragged
stepCount How many move steps to include in the drag

public static void dragViewToBottom (InstrumentationTestCase test, Activity activity, View v)

Added in API level 3

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case
v The view that should be dragged

public static void dragViewToBottom (ActivityInstrumentationTestCase test, View v)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the bottom of the screen.

Parameters
test The test case that is being run
v The view that should be dragged

public static void dragViewToTop (InstrumentationTestCase test, View v, int stepCount)

Added in API level 3

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test The test case that is being run
v The view that should be dragged
stepCount How many move steps to include in the drag

public static void dragViewToTop (ActivityInstrumentationTestCase test, View v, int stepCount)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test The test case that is being run
v The view that should be dragged
stepCount How many move steps to include in the drag

public static void dragViewToTop (InstrumentationTestCase test, View v)

Added in API level 3

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test The test case that is being run
v The view that should be dragged

public static void dragViewToTop (ActivityInstrumentationTestCase test, View v)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view and dragging to the top of the screen.

Parameters
test The test case that is being run
v The view that should be dragged

public static int dragViewToX (InstrumentationTestCase test, View v, int gravity, int toX)

Added in API level 3

Simulate touching a view and dragging it to a specified location. Only moves horizontally.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toX Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static int dragViewToX (ActivityInstrumentationTestCase test, View v, int gravity, int toX)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location. Only moves horizontally.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toX Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static int dragViewToY (ActivityInstrumentationTestCase test, View v, int gravity, int toY)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching a view and dragging it to a specified location. Only moves vertically.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toY Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static int dragViewToY (InstrumentationTestCase test, View v, int gravity, int toY)

Added in API level 3

Simulate touching a view and dragging it to a specified location. Only moves vertically.

Parameters
test The test case that is being run
v The view that should be dragged
gravity Which part of the view to use for the initial down event. A combination of (TOP, CENTER_VERTICAL, BOTTOM) and (LEFT, CENTER_HORIZONTAL, RIGHT)
toY Final location of the view after dragging
Returns
  • distance in pixels covered by the drag

public static void longClickView (InstrumentationTestCase test, View v)

Added in API level 3

Simulate touching the center of a view, holding until it is a long press, and then releasing.

Parameters
test The test case that is being run
v The view that should be clicked

public static void longClickView (ActivityInstrumentationTestCase test, View v)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Simulate touching the center of a view, holding until it is a long press, and then releasing.

Parameters
test The test case that is being run
v The view that should be clicked

public static void scrollToBottom (InstrumentationTestCase test, Activity activity, ViewGroup v)

Added in API level 3

Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(InstrumentationTestCase, Activity)

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case
v The ViewGroup that should be dragged

public static void scrollToBottom (ActivityInstrumentationTestCase test, ViewGroup v)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Scroll a ViewGroup to the bottom by repeatedly calling dragQuarterScreenUp(InstrumentationTestCase, Activity)

Parameters
test The test case that is being run
v The ViewGroup that should be dragged

public static void scrollToTop (InstrumentationTestCase test, Activity activity, ViewGroup v)

Added in API level 3

Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(InstrumentationTestCase, Activity)

Parameters
test The test case that is being run
activity The activity that is in the foreground of the test case
v The ViewGroup that should be dragged

public static void scrollToTop (ActivityInstrumentationTestCase test, ViewGroup v)

Added in API level 1

This method was deprecated in API level 3.
ActivityInstrumentationTestCase is deprecated in favor of ActivityInstrumentationTestCase2, which provides more options for configuring the Activity under test

Scroll a ViewGroup to the top by repeatedly calling dragQuarterScreenDown(InstrumentationTestCase, Activity)

Parameters
test The test case that is being run
v The ViewGroup that should be dragged

public static void tapView (InstrumentationTestCase test, View v)

Added in API level 1

Simulate touching the center of a view and releasing quickly (before the tap timeout).

Parameters
test The test case that is being run
v The view that should be clicked

public static void touchAndCancelView (InstrumentationTestCase test, View v)

Added in API level 1

Simulate touching the center of a view and cancelling (so no onClick should fire, etc).

Parameters
test The test case that is being run
v The view that should be clicked