I'm starting to study UIAutomator on Android.
I created a simple poc project, inside this there are a very few elements : one button and one editText.
The behaviour is quite simple:
When I push the botton the message written in the editText appears in a snackBar.
Now I want to make two simple test :
see if the snackbar correctly appears
see if the editTest message is
correctly reported in the snackbar
For point one I have done in this way :
#Test
public void pressEmailButton() throws UiObjectNotFoundException {
mDevice.findObject( By.res(POC_PACKAGE,"fab") ).click();
// wait for the snackBar appear
UiObject snackBar2 = new UiObject (new UiSelector().text("Send"));
// Verify the snackBarIsShowed is displayed in the Ui
assertTrue("Timeout while snackbar", snackBar2.waitForExists(1000));
}
That is i'm watching the snackbar's action to check if the snack bar is correctly opened . Are there the better ways to do that? In this way if there are more elements named in the same way of the snackbar's action I will have a problem
For the second point I don't find a way to test it.
I have to use only uiAutomator and not Espresso :)
Thanks to everyone :)
I just tried it in a new android project: I have one Button in the main layout and show the snackbar on button click like this:
button = (Button) findViewById(R.id.button);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View parentLayout = findViewById(R.id.root_layout);
Snackbar.make(parentLayout, "Snackbar Text", Snackbar.LENGTH_LONG)
.show();
}
});
In my test I then test it like this:
//With espresso:
onView(withId(R.id.button)).perform(click()); //click the button
onView(withText("Snackbar Text")).check(matches(isDisplayed()));
and the same with using UI Automator:
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject snackbarTextView = mDevice.findObject(new UiSelector().text("Snackbar Text"));
if(!snackbarTextView.getText().equals("Snackbar Text")) {
throw new RuntimeException("No snackbar!");
}
Both tests are working just fine!
Another way to select the Snackbar text would be via the resource id like this:
//replace the package name in the next line with your package name
UiObject snackbarTextView = mDevice.findObject(new UiSelector().resourceId("com.example.testespressoapplication:id/snackbar_text"));
The way you did it also works but is deprecated so I would not use it, as the documentation says:
* #deprecated Use {#link UiDevice#findObject(UiSelector)} instead. This version hides
* UiObject's dependency on UiDevice and is prone to misuse.
Related
I wanted to know if its possible to have two onclick methods for one buttton..Im trying to have a button that can open a new activity and send a id token to the server for firebase purposes, if possible how do i go about it on android studio
I think you are getting the underlying concept wrong.
Buttons react to clicks.
The "ActionListener" that gets triggered on that click ... can do whatever it wants. There is nothing (conceptually) that prevents you in your code to just trigger various things. Of course, you have to understand what you are doing (things like: not blocking the UI thread for too long; or how to kick of things in background threads, and so on).
No. There is only one onClick method for a Button. But you can still perform two different purposes by one button.
I am using a button to hide and show a linear layout. The code is given below :
final int[] count = {2};
//here startTopics is the button....
startTopics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count[0] %2==0)
{
topicLin.setVisibility(View.VISIBLE);
count[0]++;
}
else
{
topicLin.setVisibility(View.GONE);
//here topicLin is the linear layout
count[0]++;
}
}
});
It is one button and so you should apply only one onClick listener which performs the buttons job.
In your onClick-method you can just call another (private) method if you want to do multiple things without sacrificing code management.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendTokenToServer();
// Include your code to open the activity here or outsource it again into another private method
}
});
And your method to send the token to the server:
private void sendTokenToServer() {
// Your code here.
}
I dont like errors. I'm using a Popup Window to display a little hint above a TextView. the hint is for explanation of what the TextView is saying, for example RBI, will popup a hint saying "Runs batted in" I keep getting this error W/InputEventReceiver﹕ Attempted to finish an input event but the input event receiver has already been disposed. everytime i click outside of the popup hint to remove the popup, which is the way i want it to be setup, i get that error. I've tried setting the input to INPUT_METHOD_NOT_NEEDED and nothing
i have multiple hints to be shown, so i put everything in an TextView array and
i have a listener on each TextView item
method to run the popup
public void displayPopupWindow(View anchorView, String text) {
final PopupWindow popup = new PopupWindow(getApplicationContext());
View layout = getLayoutInflater().inflate(R.layout.test_popup, null);
((TextView) layout.findViewById(R.id.popup_text)).setText(text);
popup.setContentView(layout);
// Set content width and height
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
// Closes the popup window when touch outside of it - when looses focus
popup.setOutsideTouchable(true);
popup.setTouchable(false);
popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
// Show anchored to button
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAsDropDown(anchorView, 0, -225);
}
i create a listener
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = "";
...
//a bunch of code that just decides what the variable text should be
...
displayPopupWindow(v, text);
}
};
and the array of TextViews listeners is set to the listener object
//inside a for loop
TextViews[i].setOnClickListener(listener);
anyone know of anyway to rid of that error. i wouldnt want the logcat to fill up with those errors. anyone have any ideas how to fix this?
your error is a common error if you search on google,
you have to call your displaypopup method with an handler.
You can find more help here or here.
Hope i helps you
I have this in my MainActivity.java:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupHomeScreen();
}
public void setupHomeScreen() {
File latestPic = getMostRecentSnappiePicture();
if(latestPic != null){
//display pic
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
else{
layout.setBackgroundDrawable(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
//hide tutorial
findViewById(R.id.howitworks).setVisibility(View.INVISIBLE);
}
else{
//show tutorial
findViewById(R.id.howitworks).setVisibility(View.VISIBLE);
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
}
}
}
As you can see, in onCreate, it calls setupHomeScreen and checks if a file exists. If it doesn't exist, it displays a tutorial "howitworks" layout image as well as building a showcase view.
So this all works fine and well. The only issue comes when trying to leave the activity while the showcaseView is still there, OR sometimes even after you exit the showcase view and try and launch the new activity, this error comes up: ShowcaseView - width and height must be > 0
As you can see in the answers, the solution is to only create the showcase view in the callback after the original view has been created like so:
someView.post(new Runnable() {
#Override
public void run() {
// my ShowcaseView builder here
}
});
The only thing is, I have no idea where to put this, since my showcase view should only show up if the file from getMostRecentSnappiePicture() is null. So how can I put the view creation callback inside of my logic to check that that file is null first?
it looks like you're highlighting the HOME button instead of the 'howitworks' view. Try switching the line
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
with
.setTarget(new ViewTarget(R.id.howitworks,this));
Also, the following video might help. It's 20 minute tutorial on how to use ShowCaseView inside an activity with 3 buttons. He is declaring an onClickListener where he changes programmatically the TargetView highlighted by the showCaseView
https://www.youtube.com/watch?v=3zdeFSBplps
The video is in spanish, but at least you'll be able to follow the steps, since he's writing the code from scratch.
I am very new to mobile development, this isn't homework I am working ahead of my class, I developed a simple app that has a button and when it's pressed it shows a message "Hello Android". I would like to build on this and change the color of the background when the onClickListener is called, I will post my code below, I am asking for the best approach to achieve my goal (change background). I want to iterate that this code below works, and that I am not asking for anything to do with the code I have presented, I want to add to it to change the background color (it's currently white, I'm assuming by default). Oh and I have never worked with Java before (very difficult course teaching android/iOS/WinMobile in 1 class). Thank you.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupMessageButton();
}
private void setupMessageButton() {
// 1. Get a reference to the button
final Button messageButton = (Button) findViewById(R.id.helloDroidButton);
//Set the click listener to run my code.
//Code will run when user clicks button.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Anonymous class? --> not sure what he means
Log.i("DemoButtonApp", "Hello Android!");
Toast.makeText(
MainActivity.this,
"Hello Android!",
Toast.LENGTH_LONG
).show();
}
});
}
Android support feature called Selector , that help you to change the background of any view in each state of it like pressed , forces and so one , take look on this useful tutorial and feed me back in any not obvious point
http://www.mkyong.com/android/android-imagebutton-selector-example/
hope it help you
I might be on the wrong track here, and should be thinking events/publish-subscriber, if so, please enlighten me.
I have an android project running, where I have a layout which acts as an on-screen menu. Implemented in several activities/"parent-views" with the use of '< include>'. Working nicely.
Now, some of the functionality is general and global. Like I have an "add"-button, which does something, that it should always do. Then I'd like the possibility to customize what it does in addition to this, based on the activity where the action originated.
I have seperated menulogic in a simple java class, with the constructor taking an activity as a parameter. From here, I can attach clicklisteners to the buttons in the menu fine, and do stuff on click.
What I'd like is something like:
private void addBtn(String text, String path) {
LinearLayout ll = (LinearLayout) parentActivity.findViewById(R.id.dynamicButtonLayout);
Button newButton = new Button(parentActivity);
newButton.setText(text);
newButton.setTag(path);
newButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
//Do Stuff.
fireDoneHandlingButtonClick();
} catch (Exception e) {
}
}
});
}
And then have a way of handling this method in the parent activity. Should I be thinking of events, or should I be thinking of a way to add a method as an argument to the addBtn method from the activity, which can be fired from inside the click-listener?
Look at How To Implement Your Own Listener in Android or Fire and Forget Messages (events) in Android