Android espresso match view duplicated inside RecyclerView - android

I have a recycler view with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/color_primary"/>
</RelativeLayout>
I have a problem because during a test I have a moment where the recycler view show two views with the same text and I need to check if one of them is visible.
The code to match the view is the following:
onView(withId(R.id.title)).check(matches(withText("Test")));
And espresso is failing because it is finding more than one view that matchs the matcher criteria.
How can I match the specific view that I need?

One possible solution is to set a different tag in each row and use this tag to match with espresso.
Let's say the data that is being shown is persisted in the database. You can use the model id as the tag, with this you will have each row in the recycler view with a unique constraint that you can use in the matcher.
The code would be something like this:
onView(allOf(
withId(R.id.title),
allOf(
isDescendantOfA(withTagValue(is((Object)model.getId())))),
withText(model.getTitle())))
.check(matches(isDisplayed()))
Since you need to use the text inside the matcher, you can check if the view is visible to add a clause to your test.
You can read an article explaining this here: https://medium.com/tech-track/validating-views-by-tag-with-espresso-50d3f47b14a7

Related

Android Designing a custom view

Good day,
I am working on an application, and I want to design the view below, I am unsure which view to use.
From my experience with Android development, I'm thinking it should be a list view or a recyclerview with a custom header, I may be wrong, but looking at the view could someone explain what view I should use. I have red arrows indicating what I am talking about.
I was thinking a custom scrollable list view, and defining the list of items, shown by the three arrows, basically three custom list items.
Should it be a list view or recyclerview with a custom header for the arrow marked as 1.
Should the arrow marked as 3. be a gridview that's filled dynamically? As I understand it, the gridview doesn't work well with a scrollable parent, like a listview, as I want the whole view to scroll based on the content, like in Instagram, what is a good approach to accomplish this.
I came here because I have searched and really couldn't find any good advice
Thanks in advance
It seems that you don't need one single RecyclerView here. You can use a combination of ViewGroups and Views. Simple example:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Code for 1,2-->
<!-- Your grid from 3-->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</ScrollView>
Edit:
Also do this on your RecyclerView: setNestedScrollingEnabled(false).
This will make your whole layout scrollable, while your RecyclerView, having a height of wrap_content will just expand(adding to the height of the whole layout) without the need for internal scrolling.

Reuse a rendered layout in some other layout at run time

I am using a RecylerView inside a layout l1.xml. I am including this l1.xml inside l2.xml using include tag.
I update this RecyclerView after an api call but l2.xml is not showing the updated RecyclerView.
Is there a way to forcibly ask the parent to refresh?
invalidate(), refreshDrawableState(); on the parent layout didn't help?
Is there a smarter way to use a rendered layout in multiple places?
l1.xml
...
...
<LinearLayout
android:id="#+id/feed"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#color/light_primary_background">
<include layout="#layout/events_list"/>
</LinearLayout>
...
...
events_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/events_recycler_view"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
I update the events_recycler_view after an API call and the events_list.xml is updated but the include in l1.xml is not updated
Yes you can always use LayoutInflater to inflate a view, but the view must have the ids and type matching the id and type defined in your java code.
Check out this link for how to use layout inflater
http://www.programcreek.com/java-api-examples/android.view.LayoutInflater

Using the TwoLineListItem component

I have to create a list whose list items have 2 lines of text. I started building a custom list item, but then I discovered the TwoLineListItem component. I wrote this code:
pageFilterResultView=new TwoLineListItem(containerActivity);
pageFilterResultView.getText1().setText("Test");
However, getText1 returns null, and the second line throws a NullPointerException. So I thought I need to use an inflated layout instead of a constructor. The TwoLineListItem documentation specifies I can use the android.R.layout.two_line_list_item resource for the layout, so I changed the code to:
LayoutInflater inflater=(LayoutInflater)containerActivity.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pageFilterResultView=(TwoLineListItem)inflater.inflate(android.R.
layout.two_line_list_item,null);
pageFilterResultView.getText1().setText("Test");
However, this throws a ClassCastException because the layout is actually a LinearLayout. TwoLineListItem inherits from RelativeLayout, so I can't even cast the layout to a higher class in the hierarchy.
So the question is: How do I use TwoLineListItem correctly? Do I have to create my own custom layout for it? If so, what's the point of this component if I still have to do all the work of creating a list item by myself?
How do I use TwoLineListItem correctly?
The TwoLineListItem widget is a facade over two TextViews that have to be provided by you. To use the TwoLineListItem in a ListView's row you'll need a row layout where you have the TwoLineListItem widget with two(at least) TextView children with specific ids(android.R.id.text1 and android.R.id.text2). Something like this:
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#android:id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"/>
</TwoLineListItem>
Then you can use it in the getView() method like you did:
pageFilterResultView=(TwoLineListItem)inflater.inflate(R.layout.the_layout_file_above,null);
pageFilterResultView.getText1().setText("Test");
Of course you have the possibility of using an included layout file as the child of the TwoLineListItem(as long as you have the two TextViews with the required ids):
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- the android version of the two line layout -->
<include layout="#android:layout/two_line_list_item" />
</TwoLineListItem>
but this just increases the layout depth and should be avoided.
If so, what's the point of this component if I still have to do all
the work of creating a list item by myself?
Judging by the fact that you can't use this widget programmatically, I don't see the need for this component either.
put your layout code inside try catch block with ClassCastException it will works fine my Friend . .....

Android: Should I use a ListView? (case: list elements with partially unknown structure)

Assumptions about the list (updated):
It will not contain more than 10 list elements (the list elements are defined by the xml layout below).
The height of every element is unknown, because the list element contain a LinearLayout that can have up to 20 child views (see xml below).
XML Layout of the list element:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation:"horizontal">
<!--
This LinearLayout is going to contain one or more
Views which will be added progammatically on runtime.
The number of children views added to it depend on the
data to be displayed, and the only assumption that can
be made is that there's will be no more than 20 child
views for one particular instance of this LinearLayout.
-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="25dp"/>
<ImageButton
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_gravity="center|top"
android:layout_marginLeft="-25dp"/>
</LinearLayout>
Questions:
Does it make any sense to use a ListView for a layout that has such freedom
in its structure (like the one above), and still being able to make use of the
convertView as passed in to the ListView#getView(...)?
As an alternative, would it be wrong to put all the list elements in an outer
LinearLayout and put this within a ScrollView? By doing this, I wouldn't get
caching abilities of the ListView, but maybe it wouldn't be so heavy given the
assumptions about the list? (see top). (Any pointers on how to make this alternative look and feel like a ListView? I'm thinking of applying standard colors and selectors etc.)
If you know some of your 10 elemets will be the same, you could use
getItemViewType(int position)
To be sure that the convertView will match your item type convertView

Android: what is the best way to show a TextView in case if no item in a ListView?

I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.

Categories

Resources