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
Related
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.
I am trying to create a calculator. I am working in the latest Android Studio. Like in calculators, all new tokes(numbers, operators) should be shown in the right and if the field is larger than the display, it should scroll to the latest token. I have already browsed and found a way to do the same.
The code for the same is:
private void scrollRight() {
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
ViewTreeObserver viewTreeObserver = horizontalScrollView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
horizontalScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
horizontalScrollView.scrollTo(entry.getWidth(), 0);
}
});
}
scrollRight is called by a onClick event which checks if a token is entered and calls this.
Everything is working perfectly, i.e. on every new token the scroll bar is scrolling to the end. But if I try to scroll to the beginning manually, it no longer works. After this every time a new token is pressed, the scroll bar first moves to the end and then back to the beginning.
The only option that remains is to restart the program. I tried debugging the OnGlobalLayout function but the debugger loses all frames while stepping out from the function, so it is difficult to know what exactly is making the scroll bar go to the beginning.
GIF to show the problem:
Please Help!
Try
horizontalScrollView.post(new Runnable() {
#Override
public void run() {
horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
}
});
And call findViewById only once in onCreate.
I have created a edittext inside a list, which is in a Dialog. Initially edittext is disaled, by tapping on another button in dialog, i make it enabled. I debug it further and found that focus is still on background activity, which causes keyboard to come on background activity.
JUST to point out: my edit text is numeric.
Tried multiple options but no outcome:
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(null, InputMethodManager.SHOW_FORCED, null);
I found that keyboard is coming on background activity with this option after dialog open.
splitDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Both options doesn't work to open keyboard on dialog.
I have also added in editText box during XML generation.
My dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
Dialog dialog = builder.create();
builder.setTitle("Split Balance");
builder.setPositiveButton("Ok", new SplitDialogCallback());
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
splitDialog.show();
Please give directions, as i am stuck due to this issue.
When i add one EditText in dialog directly instead of list view of dialog, things work fine, but if i remove it then again same issue. Not able to understand why focus not going if edit text in list view.
The android keyboard is quite intelligent that you would not need to toggle their input in this manner.Try this once.
remove this:
InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(null, InputMethodManager.SHOW_FORCED, null);
and
splitDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
then i'd suggest using a custom dialog layout instead of a view.Try this.
final Dialog splitDialog = new Dialog(this);
splitDialog.setContentView(R.layout.abc_action_bar_decoryour_dialog_layout);
//Button that you want to toggle the editText enabled
Button btn = splitDialog.findViewById(R.id.your_button);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText editText =splitDialog.findViewById(R.id.your_edit_text);
editText.setEnabled(true);
editText.requestFocus();
}
});
splitDialog.show();
This IMHO is a better way to use a dialog,it gives you more freedom esp since you're anyways using a custom dialog layout.
I know this does not tell why your method is not working..but its merely a better handled solution
I have popup window that contains some EditTexts. I want to create my custom numerical keyboard inside the popup window. So I have 10 buttons that represent digits 0-9. Inside buttons' onClickListener I trying to dispatch key event
public void onClick(View v) {
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0));
}
but it doesn't effect to EditTexts. I tried to do it with focusable equals true and false, but the result is the same. When I am trying to dispatch key event in the main layout of activity it works well, so what I should change to make my code work inside popup window?
Have you tried requesting focus on the EditTexts? Not only using
editText.setFocusable(true);
but also
editText.requestFocus();
I think it's also possible that is not working because you're not injecting a DOWN event first. Try the following:
Instrumentation mInstrumentation = new Instrumentation();
final Thread t = new Thread() {
public void run(){
mInstrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
};
t.start();
This will inject an event which will be processed by the view that has the focus
I've been having problems with Android buttons. I try to set an onClick listener, but it fails, crashes and doesn't print any helpeul error messages. Here is my code:
Button button;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.choose_level);
}
});
I've tried putting in a try catch statement so it won't display annoying errors but the button still doesn't work. Would it be because the layout hasn't been loaded? or is it something else?
Thanks in advance.
you must call setContentView(R.layout.XML_LAYOUT); method before you callfindViewById for your button.
here XML_LAYOUT must be the Layout containing your Button ID.
Note:- it is not recommanded to call setContentView method multiple times. if you want to show a different layout/screen add it into Another activity and start that activity on button click.
you are calling setContentView(R.Layout.XML_LAYOUT) in your button onClick listener where as it should be above in oncreate method
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/
Button play = (Button)findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
show ur text here
}
});
I guess what u r trying to do is to set view for an XML file
which is some layout file i guess check out inflator and intent
I put that in a try, catch statement so it won't put annoying errors...
A catch block will not magically stop your error from occurring - you cannot use it to stop the application "putting annoying errors".
You use them to handle errors when it's possible to recover from those cases (e.g. wait and retry, fall back to a slower alternative, etc.)
What is the implementation of your catch block? If you're simply swallowing the error, your app will still fail - only you won't have any diagnostic information with which to deal with it.
You'll need to go back to your original "annoying error", work out why it was happening and then fix it rather than just suppressing its output.