Selecting Grid View Cells

Applies to TestComplete 15.68, last modified on October 03, 2024

When you work with the grid view control, you need to select a specific cell in the grid. This topic describes how you can simulate user actions on the grid view cell.

Simulating Touches

To select a grid view cell, you can use several actions provided by Android GridView object:

  • TouchCell, LongTouchCell, and similar actions – simulate touch or long touch on a specific grid view cell.

All these actions have two parameters:

  • Row - specifies the zero-based index of the row that contains the desired cell.

  • Column - specifies the zero-based index of the column that contains the desired cell.

The following example demonstrates how you can use the mentioned actions in scripts:

JavaScript, JScript

function Test()
{
  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain a grid view
  var gridViewObj = app.Layout("layout").GridView("gv");
  
  // Simulates a touch on a cell
  gridViewObj.TouchCell(1, 8);
}

Python

def Test():
  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain a grid view
  gridViewObj = app.Layout("layout").GridView("gv")
  
  # Simulates a touch on a cell
  gridViewObj.TouchCell(1, 8)

VBScript

Sub Test()
  ' Select the Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain a grid view
  Set gridViewObj = app.Layout("layout").GridView("gv")
  
  ' Simulates a touch on a cell
  Call gridViewObj.TouchCell(1, 8)
End Sub

DelphiScript

procedure Test();
var
  app, gridViewObj: OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain a grid view
  gridViewObj := app.Layout('layout').GridView('gv');
  
  // Simulates a touch on a cell
  gridViewObj.TouchCell(1, 8);
end;

C++Script, C#Script

function Test()
{
  // Select the Android device
  var app = Mobile["SetCurrent"]("MyDevice");

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain a grid view
  var gridViewObj = app["Layout"]("layout")["GridView"]("gv");
  
  // Simulates a touch on a cell
  gridViewObj["TouchCell"](1, 8);
  
}

Checking Cells Using Native Methods

Currently, TestComplete does not support checkable elements of a grid view. However, you can check and uncheck cells using native methods. This section contains detailed description of how to do this.

Cells of a grid view are represented as child objects of a grid view object. If a cell is checkable, it is a CheckedTextView object. To set this object to the desired state, you can use the setChecked native method, that sets the specified state to the desired object. For example:

gridViewObj.CheckedTextView("text1").setChecked(true)  // check the item

gridViewObj.CheckedTextView("text1").setChecked(false// uncheck the item

In order to find the needed item, you can use the ControlText property, that returns the caption of an item. Note, that items that are out of the screen are not available in the Object Browser. To reach the desired items that are out of the screen, you can use the smoothScrollToOffset native method. This method scrolls the grid view to the specified items offset. The following code snippet shows how you can use it:

gridViewObj.smoothScrollToOffset(1)  // Scroll the grid view to one item down

gridViewObj.smoothScrollToOffset(-1)  // Scroll the grid view to one item up

gridViewObj.smoothScrollToOffset(gridViewObj.ChildCount) // Scroll the grid view to the next items, that can be shown on the screen

The collection of the grid view's child objects contains only those objects that are visible on screen. When you scroll the grid view, the child objects that leave the grid area are deleted from the object hierarchy. They are replaced by other child objects that, most likely, have the same recognition attributes.

To learn if you can scroll to the specified item offset, you can use the canScrollVertically(canScrollHorizontally) method.

The following example demonstrates how you can use the mentioned methods:

JavaScript, JScript

function Main()
{
  // Select an Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain an application
  var app = Mobile.Device().Process("com.example.myapp");

  // Obtain a grid view
  var gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple");
  
  do 
  {
    // Refresh Object Browser data in order to avoid errors
    Mobile.Refresh();
    
    // Iterate through child items of the grid view object
    for (var i=0; i< gridViewObj.ChildCount; i++)
    {
      // Obtain a child object
      var child = gridViewObj.Child(i);

      // Check whether the child is needed object
      if (child.ControlText == "Item12")
      {
        // Check the needed child
        child.setChecked(true);
        break;
      }
    }
    // Scroll the grid view
    gridViewObj.smoothScrollByOffset(gridViewObj.ChildCount)
  }
  // If the grid view cannot be scrolled then the script will exit the loop
  while (gridViewObj.canScrollVertically(1))
}

Python

def Main():
  # Select an Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain an application
  app = Mobile.Device().Process("com.example.myapp")

  # Obtain a grid view 
  gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple")
  
  while (gridViewObj.canScrollVertically[1]):
    # Refresh Object Browser data in order to avoid errors
    Mobile.Refresh()
    
    # Iterate through child items of the grid view object 
    for i in range(0, gridViewObj.ChildCount): 
      # Obtain a child object
      child = gridViewObj.Child[i]

      # Check whether the child is needed object
      if (child.ControlText == "Item12"):
        # Check the needed child
        child.setChecked(True)
        break
    # Scroll the grid view
    gridViewObj.smoothScrollByOffset(gridViewObj.ChildCount)
    # If the grid view cannot be scrolled then the script will exit the loop 
    if not gridViewObj.canScrollVertically[1]:
      break

VBScript

Sub Main()
  ' Select an Android device
  Mobile.SetCurrent("MyDevice")

  ' Obtain an application
  Set app = Mobile.Device.Process("com.example.myapp")

  ' Obtain a grid view
  Set gridViewObj = app.RootLayout("").Layout("layoutTop").GridView("gv_simple")
  
  Do 
    ' Refresh Object Browser data in order to avoid errors
    Mobile.Refresh()

    ' Iterate through child items of the grid view object
    For i=0 to gridViewObj.ChildCount-1
    
      ' Obtain a child object
      Set child = gridViewObj.Child(i)

      ' Check whether the child is the needed object
      If child.ControlText = "Item12" Then
        ' Check the needed child
        child.setChecked(true)
        Exit For
      End If
    Next
    ' Scroll the grid view
    gridViewObj.smoothScrollByOffset(gridViewObj.ChildCount)

  ' If the grid view cannot be scrolled then the script will exit the loop
  Loop While (gridViewObj.canScrollVertically(1))
End Sub

DelphiScript

procedure Main();
var
  app, gridViewObj, i, child: OleVariant;
begin
  // Select an Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain an application
  app := Mobile.Device.Process('com.example.myapp');

  // Obtain a grid view
  gridViewObj := app.RootLayout('').Layout('layoutTop').GridView('gv_simple');
  
  repeat 
  begin
    // Refresh Object Browser data in order to avoid errors
    Mobile.Refresh();
    
    // Iterate through child items of the grid view object
    for i:=0 to gridViewObj.ChildCount-1 do 
    begin
      // Obtain a child object
      child := gridViewObj.Child(i);

      // Check whether the child is the needed object
      if (child.ControlText = 'Item12') then
      begin
        // Check the needed child
        child.setChecked(true);
        break;
      end;
    end;
    // Scroll the grid view
    gridViewObj.smoothScrollByOffset(gridViewObj.ChildCount)
  end;
  // If the grid view cannot be scrolled then the script will exit the loop
  until (not gridViewObj.canScrollVertically(1))
end;

C++Script, C#Script

function Main()
{
  // Select an Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain an application
  var app = Mobile["Device"]["Process"]("com.example.myapp");

  // Obtain a grid view
  var gridViewObj = app["RootLayout"]("")["Layout"]("layoutTop")["GridView"]("gv_simple");
  
  do 
  {
    // Refresh Object Browser data in order to avoid errors
    Mobile["Refresh"]();
    
    // Iterate through child items of the grid view object
    for (var i=0; i< gridViewObj["ChildCount"]; i++)
    {
      // Obtain a child object
      var child = gridViewObj["Child"](i);

      // Check whether the child is the needed object
      if (child["ControlText"] == "Item12")
      {
        // Check the needed child
        child["setChecked"](true);
        break;
      }
    }
    // Scroll the grid view
    gridViewObj["smoothScrollByOffset"](gridViewObj["ChildCount"])
  }
  // If the grid view cannot be scrolled then script will exit the loop
  while (gridViewObj["canScrollVertically"](1))
}

Using Keyword Operations

This topic explains how to simulate touches on the grid view control in scripts. You can use the described actions in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android Grid View Controls
Working With Android Grid View Controls - Basic Concepts
Android GridView Support

Highlight search results