How to Distinguish UI Controls by Color Using UiAutomator in Android
The author shares a practical UiAutomator solution that captures screenshots, extracts bitmap data, and analyzes pixel colors to programmatically determine whether a UI element appears blue, including reusable helper methods for retrieving individual red, green, blue, or full RGB values.
Detecting a UI element’s dominant blue color with UiAutomator
The following routine captures a screenshot of the device, extracts the bitmap of a target UiObject, scans every pixel inside the object’s visible bounds, computes the average value of the blue channel, and returns true when the average exceeds a configurable threshold (default 200). This technique enables color‑based assertions in Android UI tests where a control’s appearance changes under different conditions.
public boolean isBlue(UiObject uiObject) throws UiObjectNotFoundException {
// Capture the current screen to a temporary file
screenShot("test");
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
// Get the rectangle that encloses the UI element
Rect rect = uiObject.getVisibleBounds();
int left = rect.left;
int right = rect.right;
int top = rect.top;
int bottom = rect.bottom;
// Accumulate the blue component of each pixel
long blueSum = 0;
int pixelCount = 0;
for (int x = left; x < right; x++) {
for (int y = top; y < bottom; y++) {
int color = bitmap.getPixel(x, y);
blueSum += Color.blue(color);
pixelCount++;
}
}
// Compute the average blue intensity (0‑255)
int avgBlue = (int) (blueSum / pixelCount);
// Threshold can be tuned per application
return avgBlue > 200;
}Utility methods for retrieving individual color channels
For finer‑grained checks you may need the exact red, green, or blue value at a specific coordinate. The helpers below reuse the same screenshot‑capture logic and return the requested channel or the full RGB triplet.
public int getRedPixel(int x, int y) {
screenShot("test");
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
int color = bitmap.getPixel(x, y);
return Color.red(color);
}
public int getGreenPixel(int x, int y) {
screenShot("test");
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
int color = bitmap.getPixel(x, y);
return Color.green(color);
}
public int getBluePixel(int x, int y) {
screenShot("test");
String path = "/mnt/sdcard/123/test.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
int color = bitmap.getPixel(x, y);
return Color.blue(color);
}
public int[] getRGBcolorPixel(int x, int y) {
screenShot("testDemo");
String path = "/mnt/sdcard/123/testDemo.png";
Bitmap bitmap = BitmapFactory.decodeFile(path);
int color = bitmap.getPixel(x, y);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return new int[] { red, green, blue };
}Practical considerations
Performance: Scanning every pixel inside a UI element can be costly on large controls. Limit the region or sample a subset of pixels if execution time becomes an issue.
Threshold tuning: The default value of 200 works for bright blue backgrounds on typical devices. Adjust the threshold based on device screen density, UI theme, or ambient lighting conditions.
File handling: The screenshot is saved to /mnt/sdcard/123/. Ensure the directory exists and the test runner has write permissions.
Re‑usability: The utility methods capture a fresh screenshot each call. If multiple color checks are needed for the same UI state, capture once and reuse the Bitmap to avoid redundant I/O.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
