I'm working on Espresso tests for Android and for one of my actions I want to search a list using a SearchView, select an item and then navigate back to the previous screen (by closing the searchview and then pressing the up button).
I've tried some things to access the up/collapse button of the SearchView to close it (that don't work):
onView(withContentDescription(R.string.abc_action_bar_up_description))
onView(withId(R.id.search_close_btn))
onView(withId(R.id.home))
onView(withId(R.id.homeAsUp))
onView(withId(R.id.collapseActionView))
Espresso.pressBack() (to just navigate back)
I'm hoping someone might know some way to access and close this button, because I have no idea.
Thanks in advance.
Apparantly I had to press the back button twice to navigate out of it, so if anyone would like to know, you need to use Espresso.pressBack(); twice to get out of the search menu.
The correct id is R.id.search_close_btn
onView(withId(R.id.search_close_btn)).perform(click())
I was with the same problem of accessing this up/collapse button and by reading the hierarchy of components I found out that this button has the content description of 'Collapse'. So using the following solved the problem for me:
onView(withContentDescription(androidx.appcompat.R.string.abc_toolbar_collapse_description)).perform(click())
Related
Is there any way of disabling / enabling the touch events in a certain activity
without disabling certain buttons. like if the phone is "frozen" on a certain activity page for limited time?
I need that the user wont be able to go back on page, slide out or any other events.
Is it possible? (I'm using android studio, java)
Thanks in advance!
Any answer would help :)
It's possible.
Actually there is no special function to make it.
To make it, you can make a full screen button with transparent background.
And you will define a function of the button.
Of course, it will be an empty function.
Well, you can do nothing on the screen.
and then you can hide or visible the button according to your necessary.
I have created a Activity which extends FixedExpandableListActivity.
In my Activity, I am having EditText where in after entering some text, and doing a long press on the EditText, copy paste option is not appearing.
Doing multiple taps on the EditText, copy paste option pops up and disappear.
Issue is observed on 4.1.2.
Can some one please help out how to fix this issue.
My gremlin for this problem was a combination of windowSoftInputMode="adjustResize" and a View.OnLayoutChangeListener which was reparenting the EditText, for unrelated reasons. (Removing the EditText caused the copy/paste actions to disappear, sensibly enough.) Hard to track down.
How can I programatically hide the list of suggestions that pops up up below the SearchView?
There are times when I'd like the SearchView to not be inconified and to not have focus. I can do this both using the setIconified(false) and the clearFocus() methods respectively, but if the SearchView has any text in it, it will display the list of a search suggestions and I need to hide/suppress that.
searchView.setSuggestionsAdapter(null); did that trick for me.
Try to set an OnQueryTextListener to your search view, and return true on onQueryTextChange when you don't want any suggestions to show up.
If you've set a SearchView.OnQueryTextListener on your SearchView, you can return false in onQueryTextSubmit.
This will make sure suggestions get closed/hidden whenever the user submits the search query (by clicking on a suggestion or the "search" button on the keyboard).
My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.
I created a ListView in Android, and a corresponding ListActivity. Each individual item in the ListView has just one TextView (I plan to add an image and a CheckBox later).The ListActivity overrides the onListItemClick to perform certain tasks on click of any item on the list.
Heres whats happening -
When I first tried clicking on any item, nothing happened.
I then tried setting the properties "Focusable" and "Focusable in Touch Mode" to false for the TextView, as mentioned here, here and here. The List items started recognizing clicks, but only when I clicked somewhere away from the TextView. Whenever I tried clicking on the TextView or anywhere near it, it did not work.
I also tried changing various attributes like Clickable, but nothing has worked so far.
Any idea what I could be doing wrong ?
Thanks
After playing around with virtually every attribute in my TextView, I finally found the reason why it was not working. It was because of the attribute android:inputType="text" in my TextView. I'm not sure why I added that piece of code (I probably copied the TextView from one of my other applications), but removing it solves my problem.
Class which will listen clicks on ListView should implement interface AdapterView.OnItemClickListener