I am looking for some ways to programmatically call onUserInteraction. Actually when I perform any click it is called, but if I programmatically call performClick() on any of the views it is not getting invoked, although the click is performed. I also tried by sending touch events through adb.
So, my question is can I programmatically invoke the onUserInteraction by doing some interactions inside the code.
I guess I have to do something that will make the OS think it is an actual user action.
actually I want to replay some events. So when one event happens I wait for the onUserInteraction callback to dispatch another event. Otherwise the views will not be inflated properly and the next view (the action to be performed) might not be yet available. Calling this.onUserInteraction() will not solve my problem as it will be executed in the same thread.
Related
I am working on an application that will have the following feature:
The application will have a "Load Image" button to open an image and settings modal dialog. It will need to block until that dialog returns, either with the results of the processing or null if the user changed his mind.
The image and settings dialog will allow the user to select an image using a JFileChooser dialog and to specify to what level of detail to process the image. Clicking a "Load" button will open a load dialog.
The load dialog needs to be a custom-designed dialog that reports in detail about the time-consuming processing of the image. If the user allows the processing to finish, it needs to close and return the object back to the original dialog, which needs to close and return that object back to the application. If the user decides it is taking too long to perform the processing, he can cancel the load, closing the loading dialog and returning to the image and settings dialog.
Conceptually, this does not seem so difficult to me. However, when I try to determine how to get this to work within Swing, somehow I cannot put it together. From what I've read, GUI components need to be instantiated in Swing's event thread since many of them are not thread-safe. These same components need to block on calls similar to (but not the same as, since I need to write custom components) the JOptionPane.showInputDialog() methods. But these calls need to instantiate new components in the event thread and wait for events to occur in the event thread before returning a value to the application. Compounding this with the fact that I need a dialog to pop up from a dialog, I feel quite lost.
I have read the Java Tutorial on dialogs and several posts on StackOverflow and other sites trying to determine how I can design classes that work correctly. Somehow, I just don't understand how this can work at all (isn't the event thread going to sleep after the first blocking call?), and how I can write the custom classes I need to make this work. Frankly, I am not certain I understand my confusion enough that I was able to explain it.
Could someone please explain what goes on under the hood when modal dialogs have been instantiated? How I can write dialog classes that behave the way I need as described above?
The application will have a "Load Image" button to open an image and settings modal dialog. It will need to block until that dialog returns, either with the results of the processing or null if the user changed his mind.
OK, so this dialog will need to be modal. That much we know.
The image and settings dialog will allow the user to select an image using a JFileChooser dialog and to specify to what level of detail to process the image. Clicking a "Load" button will open a load dialog.
OK, so the load dialog will need to be modal off of the image and settings dialog. No biggie there either.
The load dialog needs to be a custom-designed dialog that reports in detail about the time-consuming processing of the image. If the user allows the processing to finish, it needs to close and return the object back to the original dialog, which needs to close and return that object back to the application. If the user decides it is taking too long to perform the processing, he can cancel the load, closing the loading dialog and returning to the image and settings dialog.
OK, so the load dialog code will need to instantiate and execute a SwingWorker to do the time-consuming image processing in a background thread, and then have the SwingWorker use its publish/process method pair to push information about the processing details back to the load dialog.
...From what I've read, GUI components need to be instantiated in Swing's event thread since many of them are not thread-safe.
Correct.
These same components need to block on calls similar to (but not the same as, since I need to write custom components) the JOptionPane.showInputDialog() methods.
And this is what a modal JDialog allows you to do. Another option to keep in mind is to use a JOptionPane and pass in a JPanel with whatever GUI you want the JOptionPane to display. JOptionPanes are surprisingly flexible and useful.
But these calls need to instantiate new components in the event thread and wait for events to occur in the event thread before returning a value to the application. Compounding this with the fact that I need a dialog to pop up from a dialog, I feel quite lost.
Again it's simple. The load dialog will call a SwingWorker which will communicate back to the load dialog.
Could someone please explain what goes on under the hood when modal dialogs have been instantiated?
Now you may be asking a bit too much for the volunteers on this site to do, since this question would probably require someone to write a complete tutorial to answer, and it has been asked and answered before, so the information should be discoverable by you. If you really want to see what is going on under the hood, you should first do the preliminary research on the subject yourself, look at the source code, and if still stuck, ask a much more specific and answerable question after first doing your own due diligence work.
Modal dialogs started from the primary event loop spawn a secondary event loop that remains active while the primary loop is blocked. See java.awt.SecondaryLoop.
I am developing an application in Android which has several Activities and need to check some details only when application is minimized and maximized, not on Activity screens' navigation. onPause() and onResume() doesn't work. onWindowFocusChanged() also doesn't helped as that is also calling while navigating among screens. Please help in which way I can be able to get an event only when Application is minimized/maximized, not on screen navigation.
I think that this Activity callback onUserLeaveHint() could be a start.
from the doc:
Called as part of the activity lifecycle when an activity is about to
go into the background as the result of user choice. For example, when
the user presses the Home key, onUserLeaveHint() will be called, but
when an incoming phone call causes the in-call Activity to be
automatically brought to the foreground, onUserLeaveHint() will not be
called on the activity being interrupted. In cases when it is invoked,
this method is called right before the activity's onPause() callback.
This callback and onUserInteraction() are intended to help activities
manage status bar notifications intelligently; specifically, for
helping activities determine the proper time to cancel a notfication.
It's not something which is common on Android, so I would agree to CommonsWare, that you should consider a different approach.
Nevertheless, there is a (bit hacky) way to achieve this.
Override onStop() and onResume() in every of your activities, set a boolean to true in onResume() and to false in onStop(). If this is true, the application is active, if it's false it's not.
You could use a listener, which is always called after the boolean value is set, to execute your actions.
If you can add a property, let's name it isShown in your customized Application object, which means you need extend the application for this purpose. it is in your subclass of Application and it is a signleton.
Be realized the lifecycle methods for onPause and onResume, when the onPause is called, it means the activity would be going to background, invisible any more, or put into the the activity stack. onResume means the activty status is recovered and ready for the user's interaction. So if you set the isShown property to false, which is in your application object, within the activity's onPause, and set it back to true in your onResume. of cause, you can make this behavior to be extracted into a superclass for the reuse purpose.
with the composite of onPause and onResume, and a given time we can identify the min/maxmized status. Let's say, isShown is set to false for a while, we can say it is minimized. otherwise, we can explain the maximized too.
But what are you going to do with this flag? did you still have a Service running in backend ready to receive this property to do something?
GXT 3.x only.
It is becoming apparent to me that Sencha had deliberately designed FileUploadField to shunt off all key press events from ever being detected.
I tried to intercept onBrowserEvent(Event) and could not detect any key press events which I would have generated by keypresses while having focus on the FileUploadField component.
Where is the key-press event shunt?
I could not find any keypress handler insertion methods.
I wish to allow triggering the file upload by either press on the space-bar or enter key.
Short of rewriting a whole new component from scratch, could someone advise me what I could do to achieve my goal of a keyboard activated file upload?
onBrowserEvent won't recieve any events unless you sink them - did you make sure to call sinkEvents? How are you adding handlers? If you use addDomHandler, it will sink them for you, but addHandler either assumes that they are not dom events, or that you already called sinkEvents. Without sinking an event, the browser doesn't know to pass that event on to a GWT widget. If all events were sunk automatically, then every time you moved the mouse across the page you would see a firestorm of events as mousemove fired for every widget you passed, and all of its parents.
If you override onBrowserEvent, then you are building the method that describes how to handle the actual event that comes from the browser - that is where the com.google.gwt.user.client.DOM class wires into the Widget to give it events. Short of making that method final, there is no way to prevent you, the widget user, from getting those events as long as the browser is generating them and passing them through the event listener.
Even if onBrowserEvent has been overridden and made final, you can still get access to many events by creating a NativePreviewHandler and checking where the event is occurring. This gets you in to the event before it even goes to the widget itself - there you can call NativePreviewEvent.cancel() to prevent it from happening on the widget itself, or you can handle it early in the handler.
I have an app structure like this:
Background: The Parent activity #1 is just holding the tab host, tab widget and is also setting up the 3 tabs to have their content set to the 3 tab activities (tab1, tab2, tab3 activities).
Update: I tried calling my validation method inside
onTabChangedListener in Parent Activity #1 but I got a Null Pointer
Exception. Couldn't really trace it to anywhere. I did
commented/deleted conflicting code but still I am not getting the
bottleneck.StackTrace(PasteBin Link).
Code for Parent Activity #1,Tab#1
Activity
Problem: I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener. Am I missing something here? The listener(s) are set in their own tab# activities under oncreate method. Apart from trying the above technique, I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.
Idea behind validation being: I want that while the user is changing tabs, the data should be validated before he can skip over a tab. So, ineffect I would require tab#1 to validate data in a event similar to onTabChanged if tab#2/tab#3 is selected. Also, this would apply if current tab#2 is selected and user selects tab#1/tab#3
Any advice will be appreciated..Thanks for reading..
I want to validate data entered by user in the form field(s) in the
individual tab activities onTabChanged event but I am unable to set
more than a single setOnTabChangedListener.
There is no need for a second OnTabChangeListener and even if you could set it it wouldn't help you. As you constructed the code you need access to the child activities. You can do this by using one of the answers in this question. The problem is, that those answers, except the accepted one, use deprecated methods.
My method, that I proposed in the comments is to have a static boolean field in each of the child activities used as tabs and let all of your activities update that boolean flag whenever there is a change of state of the views in those activities(if you check a CheckBox, enter something in an EditText etc). Then you can simply check the flag for the desired child activity in the OnTabChangeListener. My method should work but your code is a bit messy so you would have to modify it quite a bit.
I had tried setting up the listener in onResume() under the main
Parent activity #1. But the on Resume() method was never invoked. I
got a null pointer exception too.
It's normal that you get a NullPointerException with your code as I haven't seen where you initialize the references to the child activities that you use in the OnTabChangeListener.
Also:
Don't use TabActivity. It's been deprecated in favor of the Fragments framework which is more flexible. Those fragments could help you because, I think you want to stop changing the tabs if the validation of the current page fails and the OnTabChangeListener might come a bit late for that(but I may be mistaken about what you want).
As a side note, use equals in your code to test String equality and not ==.
Here is the scenario. I have an swing applet with tons of checkboxes. some of them are disabled/unchecked when checking another. Each ItemStateChange() event executes a method to parse the entire form for changes. Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
The ItemStateChange() for each checkbox has the standard parameter java.awt.event.ItemEvent evt
I'd like to only call the processOrder() method once when a box is clicked. Right now it fires for each change thats made, regardless of whether the change happened from setSelected(). Sometimes there are 10+ parseForm(); calls from a single click.
You can't tell whether the source of the event is a mouse click or a setSelected call from the ItemEvent.
It sounds like you have a loop in your check box logic. You might want to add a controller that handles the events and sets each checkbox yet ignores events that occur due to calling setSelected on other check boxes.
Is there a way to tell if an ItemStateChange() event was triggered due to a mouse click or from a setSelected() call?
If your application manually invokes the setSelected() method then you can use code like:
checkBox.removeItemListener(...);
checkBox.setSelected(...);
checkBox.addItemListener(...);
If you are able to change to use a MouseListener instead of an ItemListener and respond to the mouseClicked() event you will only receive the events for the checkbox selected by the user.