UiAutomator Accessing a child with limited information - android

I have a list of view that are built dynamically so they all have they exact same Resource id, and no description. The only way I can distinguish the difference between them is a text that is in the same parent. I am thinking the only way would be to do it like this:
Child >> parent >> child
As you can see above, I can get the child MILES or CALORIES. I need to get the text from the large numbers.

The text from the large numbers views can be obtained. Let's suppose the layout is:
1. RelativeLayout
0. Text View -> MILES
1. Text View -> 0.50
2. RelativeLayout
0. Text View -> CALORIES
1. Text View -> 100
UiCollection resultsPage = new UiCollection(
new UiSelector().className(android.widget.ListView.class.getName()));
UiObject resultLayout = resultsPage
.getChildByText(new UiSelector()
.className(android.widget.RelativeLayout.class
.getName()), text); //where text can be MILES or CALORIES
UiObject score = resultLayout.getChild(new UiSelector()
.resourceId(the resource id for the large text));
return (String) score.getText();
The idea is to isolate the two Relative layouts and search for the text within them, not the whole GUI.
You can use uiautomatorviewer to look at the GUI layout. Maybe you could update the question with the detailed layout.
I could use that in order to give a more accurate answer.

Related

Recycler view items take its width and can continue with new row

is there any way to make a design like this (image)
EXPLANATION:
List of items where item take its width if screen widths are not fit so continue in next row.
Item(2) starts after the item(1) and continues in the next row.
Items will be textviews
flexlayout has limit if item will not fully fit it make it in another row
Edit:
(Assume Case )
assume I have two texts and want to display them
for ex text1 = "Hello ", text2 = "Ahmed"
and screen width can fit 8 character
so UI should be
Hello A
hmed
[Actual Case]
here there are two texts , first with below background and second without this is final UI
A small example for assuming the case
input: Hello Ahmed
expected output :Hello A
hmed
You can store a list of strings into single string. Make text view height wrap content using Spanable string, you can have more control on text.
You can achieve that by adding android:scrollHorizontally="false" to the TextView xml that is displaying the text.
<TextView
.
.
.
android:scrollHorizontally="false" />

How to dynamic insert elements of a string array into textview and position it?

I get the list of elements from the HTTP response, then I want to dynamically insert that list into the textview inside the "box" that you can see, currently it just inserts a string and overlaps them one over the other. I tried changing the layout (all three constraint, relative and linear) and it didn't help. Does anyone know how to position them dynamicly inside the boxes and not overlap but have margins like in the second picture? Otherwise, inside the project, I use a constrain layout.
Here is my code:
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.layout);
int size = response.toArray().length;
final TextView[] tv = new TextView[size];
TextView temp;
for (int i = 0; i < size; i++)
{
temp = new TextView(Activity.this);
temp.setText(response.get(i).getName());
parentLayout.addView(temp);
tv[i] = temp;
}
Here is the picture how it looks right now:
And here is the picture how I want it to looks like:
This sounds like a typical ListView use case.
Firstly, I'd suggest you go through the documentation -
https://developer.android.com/reference/android/widget/ListView
You can see an implementation example of a list view with an array of strings here -
https://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65
In general, you choose the UI of your item and the listView populates the view to each item in your list (each string in your case).
In the adapter, you give each item the data it needs for the UI.
I would suggest you to use RecyclerView for this type of task. You may use ListView as well.
But RecyclerView is more flexible and advanced than ListView.
Create a simple layout or xml file for your row item to be shown in RecyclerView.
Add that row xml file in onCreateViewHolder method. And inside method onBindViewHolder do necessary task like for example, showing name in the list for each position.
Go to this link for your reference : https://developer.android.com/guide/topics/ui/layout/recyclerview
Instead of Array<String> you can use Array<CustomModel> as well depending on your requirement.
Simple example of RecyclerView with model objects as list : https://www.javatpoint.com/android-recyclerview-list-example
Well, the proper way to do what you need is use ListView or RecyclerView.
Anyway, if you want to use your current solution, you need to specify the position of each TextView.
For example, assign an ID to each textview you create and then set the position of it under the previous one. Here you can find how to do that.

Set Custom layout inside ListView as non-scrollable in android

I have a ListView which contains a custom layout.
Each row of the list View look like this:
The row contains 2 LinearLayouts, one for Date and one for Progress details. The progress Details Layout consits of 2 TextViews(Heading and data below it) and 1 Button (View More).
When the user clicks 'View More' button the data below the heading expands to 10-12 lines.
My problem is that when the TextView expands, a scrollbar comes at the edge and the user has to scroll to read. The width of the row does not change i.e the row does not expand.
I want the row width to expand so that the user does not have to scroll to read the text.
I did read a lot and have already tried the following options but they did not work
1. android:scrollbar="none"
2. View.setOverScrollMode(View.OVER_SCROLL_NEVER);
3. View.setScrollContainer(false);
Please help me with this.
You have to use exapanding listview animation for that you can use this lib for that
https://github.com/nhaarman/ListViewAnimations
Use a expandable List View such that the normal view of your list will have the image that you provided and on clicking that view it will expand to reveal the details that you wanted to show by clicking the view more option(in your image)
Here is a tut link for more info.
Try to use LayoutParams and change the height programatically inside the listener for you Button.
LayoutParams should derive from the type of layout containing your LinearLayout. If for instance it is a RelativeLayout, it will look something like that:
int heightForRow = 100;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.height = heightForRow;
yourLinearLayout.setLayoutParams(params);
Please note that your LinearLayout must then be inside another layout (RelativeLayout in the example)

GridLayout change only one specific element?

I have GridLayout 7x7, is it possible to change content only of cell (3,4) and I want to do it dynamically.
Example: I have 7x7 grid of buttons and want to change button on position (3,4) to TextView.
EDIT: Also, is it possible to set zero padding around child?
Thanks!
In GridView you cant access elements specific by coordinates (3,4).
The elements are numbered like in 2D arrays. So your (3,4) element will have (7*3+4)-1 index. And you can access it by.
ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(24);
EDIT: Set padding to zero.
gridChild.setPadding(0,0,0,0);

How to add blocks of linearLayouts and is there a better way?

This is the main idea of what I am trying to do.
I would like to ask you what is the best way to make such a design. the problem is that these gray/black blocks might not show up (user will choose which should show up). So I would like to find out do I need to make these 3 text views inside linearLayout programatically? Is there anyway to create some sort of template which I would only have to edit by setting new texts for textViews and add them to some sort of layout?
Another option is to just have a LinearLayout and an xml for the row entry. Then you can do:
LinearLayout layout = (LinearLayout) this.findViewById(R.id.your_layout_id);
List<Blocks> userBlocks = getMyUserBlocks();
for(Block b : userBlocks) {
View blockView = LayoutInflater.from(getBaseContext())
.inflate(R.layout.your_row_layout, layout, false);
TextView someData = (TextView) blockView.findViewById(R.id.your_text_view_id);
someData.setText(b.someAttribute.toString());
layout.addView(blockView);
}
You will need a separate xml layout for the block row.
You can use a ListView with large dividers. Your dark gray blocks are the row views, and you can just append them to a dataset and update the adapter for the ListView. For the dividers, see setDivider() and setDividerHeight().

Categories

Resources