I want to make a touch on a button in a layout and also that touch is done by program using injectinputevent method, so that button is clicked even without touching it.
You can use the performClick method on that button.
Call this view's OnClickListener, if it is defined. Performs all normal actions associated with clicking: reporting accessibility event, playing a sound, etc.
You can call if from another method:
your_button.performClick();
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 would like to set FLAG_NOT_TOUCHABLE on a transparent activity to allow interaction with the activity below. Is there any way to close the transparent activity when a user touches the screen while these flags are set?
I do not believe you'd be able to determine if the user touched the screen if FLAG_NOT_TOUCHABLE is set. Your Activity will simply not receive any touch events at all.
Instead, what you can do is set an onTouchListener to the root View, then return false indicating that you do not want to handle the touch. This way, you'll always receive the first touch event (ACTION_DOWN) which allows you to call finish() and go.
yes, you can.
// not any touch event
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
I'm playing with Android and I'd like to know if there's any way to invoke a listener call programmatically, for example, having an onClick() listener and invoke a call to this listener without touching the screen when the activity is created.
There is no way to get the set OnClickListener. So you need to store it and call your OnClickListener directly.
OnClickListener store = new OnClickListener() {/*...*/};
view.setOnClickListener(store);
store.onClick(view);
Never tried that, but after assigning a clickListener to your object (for example a Button), call on your onCreate method myButton.performClick().
Android doc :
public boolean performClick ()
Added in API level 1
Call this view's OnClickListener, if it is defined. Performs
all normal actions associated with clicking: reporting accessibility event,
playing a sound, etc.
Returns
True there was an assigned OnClickListener that was called,
false otherwise is returned.
Although this is possible, I'd actually advise against it. Listeners should be called from the UI but the business logic behind it is what should actually be called directly. This would provide "separation of concern" between both layers.
You should be calling the code that the listener calls in it's onClick method rather than invoking the onClick directly.
I know this may not be an appropriate question, but I want to know how a touch enabled OS like
android detects a button in an app and calls the appropriate handler? When programming an app we just give the alignment information of a particular button. So, how android keeps the mapping between a button and screen position? I think this is kindof dynamic because if you change the screen orientation or use zoom or something like that the button positions are changed dynamically. So, android must look at the touch position and decide whether a button is there or not and call the appropriate handler. How this all things put together?
I appreciate any reply.
What is the name of a event that is called once he user stops touching the screen for the View object?
onTouchEvent fires with a type of ACTION_UP.
You can check here to see all available events: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP
The one for you is ACTION_UP.
Hope this helps!
Update:
Moreover when you move outside the View without moving up your finger, ACTION_CANCEL is called. So you should handle both.