So first off, I got this snippet of code working in an empty activity, it just calls for every time when I touch the device.
#Override
public boolean onTouchEvent(MotionEvent ev) {
System.out.println("touch");
return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
But as soon as I apply this snippet of code to an activity which also has a GridViewPager, the listener doesn't report any touch events.
So my guess is, that all touch Events are getting blocked by the GridViewPager and my listener is not getting anything.
Is there a way how I can fix that?
Related
As the title says.
Specifically, I am writing an app that prints data to files over the course of runtime. I want to know when I can tell my PrintWriters to save the files. I understand that I can probably do autosave every X minutes, but I am wondering if Android Studio will let me save on close instead. I tried using onDestroy but the code block never executed. (To be precise, I started the app, did a few things, closed the app, clicked Recents, and swiped the app away. The debugger showed that the app never got to that code.)
My current solution attempts to catch the surrounding circumstances by checking for key presses but this only works for the back and volume buttons and not the home, recent, or power buttons.
#Override public boolean onKeyDown(int key, KeyEvent event) {
close();
return false;
}
There's a built in hook to the Activity lifecycle to save your state- onSaveInstanceState. There's even a bundle passed into you to save your state into for it to be restored (the matching function is onResumeInstanceState). And as a free bonus, if you call super.onSaveInstanceState and super.onRestoreInstanceState, it will automatically save the UI state of your app for all views with an id.
Please check the activity lifecycle:
https://developer.android.com/guide/components/activities/activity-lifecycle
Or if you're using a fragment:
https://developer.android.com/guide/fragments/lifecycle
Consider using one of these two:
#Override public void onStart() {
super.onStart();
close();
}
#Override public void onPause() {
super.onPause();
close();
}
My name is Bruno.
Unfortunately I am having a problem with key pressing.
The problem:
When I press any key from the keyboard and keep it pressed, the function onKeyUp() is called
without releasing that key. The correct behavior is: when we press a key, the function onKeyDown() is called and when that key is released, onKeyUp() is called.
I tested some Android emulators and only some of them this problem occurs. I want to use Android Studio Emulator. For example, Bluestacks this problem don't happens. I don't know why this occurs on some emulators.
Here is my code, MainActivity.java:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyUp(keyCode, event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("KEYTEST", event.toString());
return super.onKeyDown(keyCode, event);
}
When I press 'D' key and keep it pressed, then log prints:
log image
What is happening? There a problem with certain emulators in combination with portuguese keyboard?
Don't use onKeyUp and onKeyDown for letters. Most keyboards don't use them, they send data via the commitText() function of the InputMethodConnection. That completely bypasses the key up/key down system. Those are only reliable for physical buttons. Like the volume buttons, or a hardware keyboard attached via bluetooth.
I am trying to build an Android TV application using Android Studio that supports channel up/down buttons as an extra features to navigate to different YouTube channels/shows/videos (like a normal satellite TV would do) rather than only supporting the D-pad navigation buttons. Is it possible to achieve this & hopefully anyone had an idea how to do this?
Thank you.
While waiting for someone to reply to this question, I've discovered the solution myself.
Basically you just need to Override onKeyDown() or onKeyUp() method in related Activity class, and don't forget to return super.onKeyDown() or super.onKeyUp() so unattended KeyEvent will be attended as normally it will.
As for my case, I'm trying to have custom features for channel up & down buttons only. So below is the sample code.
#Override
public boolean onKeyDown(int KeyCode, KeyEvent event){
boolean handled = false;
if(KeyCode == KeyEvent.KEYCODE_CHANNEL_DOWN){
Log.i("KeyEvent","Channel down button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
else if(KeyCode == KeyEvent.KEYCODE_CHANNEL_UP){
Log.i("KeyEvent","Channel up button pressed");//for debugging, to be printed on logcat
handled=true;
//do something
}
if(handled){
return handled;
}
//return super.onKeyDown() to attend unattended KeyEvent
else{
return super.onKeyDown(KeyCode, event);
}
}
KeyEvent class documentation that contains list of KEYCODE constants available can be found here.
Hi My requirement is like this. I have an activity in that I want to disable back button, menu button, home button screen settings button. When I touch on that screen I open one alert dialog with two buttons. If I click ok button all the buttons are enabled and my app is exit.
Thats my requirement. If anyone knows about this, please reply me as soon as possible. Quick reply is Very very appreciable.
To achieve this you must specify your activity as android.category.HOME.
And when you finish your activity (press ok button) you must start default launcher on phone.
But it's mean that on every home button press you will see your activity. To prevent this you must determine how your activity was started.
I would rather override onKeyDown method
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
//DO NOTHING
}if(keyCode == ..etc){
//Still do nothing
return super.onKeyDown(keyCode, event);
}
This way you will have full control of what happens.
Regards,
Robert
I'm writing a game for OUYA and Android and I'm using the trackpad on the OUYA controller. When ever you touch it a mouse pointer comes up and I can't find a way to hide it. I image this would be a problem for games on an Android netbook as well.
Has anyone found a way to interact with the cursor instead of just listening for events?
This won't hide the mouse, but it will at least help prevent touch events from interfering with your joystick processing code -- not a proper solution I know, but still might help people who land on this page:
public boolean onGenericMotionEvent(MotionEvent event) {
if ( (event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
//handle the event
return true;
}
else {
return false;
}
}
Android currently does not expose any functionality to hide the mouse cursor. Whenever you have an external pointing device (ie. usb/bluetooth mouse, trackpad, etc) a mouse pointer will appear on the screen whenever you interact with the device.
Unfortunately (as of JB 4.2.2) this means it is impossible without a modified ROM.
It is possible to request pointer capture now. You need to explicitly request capture:
fun onClick(view: View) {
view.requestPointerCapture()
}
As documented:
Android delivers pointer events from sources other than the mouse normally, but the mouse pointer is not visible anymore.
You can either handle pointer events by overriding onCapturedPointerEvent:
override fun onCapturedPointerEvent(motionEvent: MotionEvent): Boolean {
// Get the coordinates required by your app
val verticalOffset: Float = motionEvent.y
// Use the coordinates to update your view and return true if the event was
// successfully processed
return true
}
or registering an event handler for OnCapturedPointerListener:
myView.setOnCapturedPointerListener { view, motionEvent ->
// Get the coordinates required by your app
val horizontalOffset: Float = motionEvent.x
// Use the coordinates to update your view and return true if the event was
// successfully processed
true
}
And it's up to you to release the pointer when you're done:
override fun onClick(view: View) {
view.releasePointerCapture()
}
I know that the context of this question overall may not apply (ie: Ouya development), but this was the first search result when I looked into how to do this myself. So I figured that I'd update the answer!