How to do UI automation in android?

You should use it like python script. Example:

import sys
import os
import time
import shlex
import subprocess

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection(99, ANDROID_SERIAL_NUMBER)

def Click(device, left, top, duration = 0):
    if duration == 0:
        device.touch(left, top, MonkeyDevice.DOWN_AND_UP)
    else:
        device.touch(left, top, MonkeyDevice.DOWN)
        time.sleep(duration)
        device.touch(left, top, MonkeyDevice.UP)

def Drag_example():
    device.drag((100, 200), (1500, 150), 1, 10) 

def Settings_menu():
    package='com.android.settings'
    activity='.Settings'
    component_name=package + "/" + activity
    device.startActivity(component=component_name)
Settings_menu();

For run script use: monkeyrunner script_name

Here is: Click it's function for clicking on screen in x and y position;
Drag_example simple example of dragging
Settings_menu simple example of running activity

Don't forget to change ANDROID_SERIAL_NUMBER to your serial number.
You can get it nu,ber by adb devices command.

For more information you can read google documentation.

For using in Java read this post


Some useful link,

  1. Android: Sample Code To Demonstrate Monkeyrunner

  2. Android Guide: monkeyrunner Overview

  3. Android Guide: MonkeyDevice

  4. Android Guide: MonkeyRunner

  5. Monkeyrunner: interacting with the Views Last link but best...