Spinner

Spinner is used to allow user to select a value or data from given list of options. It's an element that has a drop-down list of options. The user can select required options from the list. There are several combinations of spinners like, default/simple spinner or custom spinner. In this chapter we will see how to select an item or option from simple spinner.

Let's assume, you have to select an option from a dropdown or spinner element, a simple solution is to first click on the spinner and open the drop down list, then select the desired option from the list.
If the list is small and the options are displayed within the limit of the screen of the device without any scrolling, you can simply use two click method to accomplish the same.

Java
C#
Python
Javascript
Kotlin
Copy
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
RemoteWebDriver driver = new AndroidDriver<>(appiumServerUrl, options);

//Click and expand the drop down options
driver.findElement(By.id("spinner")).click();

//Click on the desired options
driver.findElement(By.xpath("//android.widget.CheckedTextView[@resource-id=\"android:id/text1\" and @text=\"USA\"]")).click();

driver.quit();
C# code coming soon
Python code coming soon
Javascript code coming soon
Kotlin code coming soon

If you want to select a value from a long list of options, by scrolling the list, then you have to perform the scrolling to the element and ten need to click on the option. In the following code, we are using androidUIAutomator to scroll to element and click on desired option from the spinner.

Java
C#
Python
Javascript
Kotlin
Copy
UiAutomator2Options options = new UiAutomator2Options();
options.setPlatformName("Android");
options.setDeviceName("Pixel_4_Emulator");
options.setAutomationName("UiAutomator2");
options.setApp("/path/to/your/app.apk");

URL appiumServerUrl = new URL("http://localhost:4723");
RemoteWebDriver driver = new AndroidDriver<>(appiumServerUrl, options);

//Click and expand the drop down options
driver.findElement(By.id("spinner")).click();

//Click on the desired options
driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"India\").instance(0))")).click();

driver.quit();
C# code coming soon
Python code coming soon
Javascript code coming soon
Kotlin code coming soon
Note:

The above code can also be used for selecting an item from list view.