I am new to Android Espresso library. Trying to test click on RecyclerView item inside the ViewPager. I've searched the Internet but found nothing about ViewPager. So my question is: how to access RecyclerView inside a ViewPager using Espresso to perform click?
Let's suppose you want to tap on buttonClick, which is the id of a button inside your RecyclerView.
I use this code:
onView(allOf(withId(R.id.buttonToClick), isCompletelyDisplayed()))
.perform(click());
This gets the view with this id which is currently displayed and performs a click on it.
Hope this helps.
P.S.: Also, on espresso-contrib there are RecyclerViewActions that maybe you can use: actionOnHolderItem, actionOnItem and actionOnItemAtPosition. You can do something like:
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
Related
I am trying to create a like and dislike button. I use Checkboxes to do so.
In the XML code I have two checkboxes one called like and the other dislike
I'm trying to toggle between the like and dislike buttons. Such that they both cannot be switched on at the same time.
public void onLike(View view) {
if (dislike.isChecked()) {
dislike.setChecked(false);
}
Toast.makeText(this,"liked",Toast.LENGTH_SHORT).show();
}
The issue that I am having is that set setChecked(true) is not doing anything.
For more context, the XML for the checkbox is defined inside a fragment that has a cardview. Each item in the card view has the checkboxes.
the way I initialized the checkbox in the main activity is as follows: -
View cardViewLayout = getLayoutInflater().inflate(R.layout.text_row_item,null);
like = (CheckBox) cardViewLayout.findViewById(R.id.like);
dislike = (CheckBox) cardViewLayout.findViewById(R.id.dislike);
any ideas what's going on?
ok, I've figured out the solution. Since I am using a recycler view with a custom adapter I need to bind the onClick listener via an interface.
Here is a link to another post that will show the necessary steps to implement click listeners in adapters: https://stackoverflow.com/a/49969478/11379938
Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.
I made a news Reader App (WebView). I don't know how to add a pull to refresh in my app. I am a student who just got into programming (Learner/n00b) Anyone here that can kindly let me know what to add or which java class or xml code i have to post here so you can help me out.
You need to wrap your WebView with a SwipeRefreshLayout. To do so go to your xml file and do the following:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView.../> <!-- your view goes here -->
</android.support.v4.widget.SwipeRefreshLayout>
Now go to the java file where you have the logic to your WebView (Activity or Fragment) and add the following code:
// Swipe to Refresh
SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipeToRefresh);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Insert your code here
webView.reload(); // refreshes the WebView
}
});
This will set the OnRefreshListener and you can add your code on the method. Check out the official documentation as well to see what other methods the SwipeRefreshLayout have.
Wrap your WebView with a SwipeRefreshLayout. Add an OnRefreshListener to that SwipeRefreshLayout that calls reload() on your WebView.
I was trying with SwipeRefreshLayout but got confused. This is what helped me: In Android WebViewGold, within Config.java, just turn ENABLE__PULL_REFRESH to true – hope this helps :-)
I'm trying to use SmartTabLayout from here https://github.com/ogaclejapan/SmartTabLayout, and i have a drawer. When i'm clicking at item on drawer, i replace my fragment, to fragment with smarttablayout. For first time it's ok, smarttablayout have tabs with content, but when i click in drawer on the same item, content of tabs just disappear, and to show them back i have to scroll to the end, and scroll back, to show them. I don't have idea why.
Notes: If using fragment inside a ViewPager, Must be use Fragment#getChildFragmentManager().
https://github.com/ogaclejapan/SmartTabLayout#pageradapter-for-fragment-based-page
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getChildFragmentManager(), FragmentPagerItems.with(this.getA())
.add(R.string.item_pager_title_products, ProductListFragment.class)
.add(R.string.item_pager_title_shops, ShopListFragment.class)
.create());
FYI: Had same problem because I didn't read the manual... Original answer found here https://stackoverflow.com/a/25525714/1257369
Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.