Search code examples
javaandroidandroid-layoutarraylistandroid-uiautomator

How to extract text from Relative Layout and store it in an ArrayList


I was trying to write a UiAutomator test for an application. Here I have a bunch of textviews inside relative layouts from which I want to extract text and store it in a ArrayList. So that I can compare that ArrayList to the expected result list.

Any Suggestions on how to do it?


Solution

  • You can iterate over all children of a RelativeLayout and get its text.

    for(int i=0; i< relLayout.getChildCount(); i++) {
        TextView textView = (TextView) relLayout.getChildAt(i);
        String text = textView.getText().toString(); //put text in an ArrayList<String> as per your need
    }
    

    Hope this helps you.