I use ViewPager for a wizard flow in the application and I want to disable page switches from any user input (How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?).
Recently I discovered that when I run the application in an emulator I can still swipe page with PC keyboard left and right arrow keys.
Can I disable it? Is it a real event that can happen on a real device?
Well, I can't reproduce your issue in a ViewPager based project running on the emulator, but since your already extending the ViewPager, you can try overriding the method below to ignore the right and left keypresses.
Untested:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
switch( keyCode ) {
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_LEFT:
return true;
}
return false;
}
I would imagine that this is possible on hardware if the user has a physical keyboard connected.
Update
Try this in your extended class:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch( event.getAction() ) {
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_LEFT:
return true;
}
return super.dispatchKeyEvent(event);
}
Related
All the Android experts,
I'm working on Android TV app.
I face problem on Dpad navigation.
i would like to stop auto Dpad navigate while KeyDown UP and DOWN.
i wrote a listener on a focusable TextView, if TextView on key UP then scroll UP the listview, and key DOWN scroll DOWN.
but the below code seen failed to scroll my listview, my focus move to other focus point while i press DOWN.
Is there any solution that i can override the auto focus navigation? i would like my TextView ignore the auto navigation to next focus while i press key UP and DOWN.
Thank you.
textView.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PAGE_UP:
listview_scrollUP();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_PAGE_DOWN:
listview_scrollDOWN();
break;
}
}
return false;
}
});
After some research, i got some idea to solve my problem.
i used setDescendantFocusability() block the rest of my fragments(viewgroup) while my dpad focus enter the targeted fragment, ideally is to space out the key UP and DOWN for my purpose usage (without processing dpad navigate next focus).
textview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
LinearLayout menu = (LinearLayout)getActivity().findViewById(R.id.mainMenuLayout);
menu.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
}
}
});
my targeted fragment just needed LEFT and RIGHT to let dpad change to next focus, and then my UP and DOWN onKey Down is used for scrolling the listview.
setDescendantFocusability() enable while the dpad navigate focus reached the edge of my fragment focusing point.
textview.setOnKeyListener(new View.OnKeyListener(){
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
LinearLayout menu = (LinearLayout)getActivity().findViewById(R.id.mainMenuLayout);
menu.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
menuItem1.requestFocus();
break;
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PAGE_UP:
listview_scrollUP();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_PAGE_DOWN:
listview_scrollDOWN();
break;
}
}
return false;
}
});
this is what i came out of to solve my problem.
wish this info help up other.
thank you.
I am trying to catch events generated by the arrow keys (UP, DOWN, RIGHT and LEFT) and disable them. Below code snippet is from one of the activity class.
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.KEYCODE_DPAD_DOWN) return true;
else return true;
}
However, with those code in place, key navigation is working. I tried adding key listener to activity which doesn't work either.
The target device is Samsung GT-I5500 with Android 2.2 version on.
Am I missing anything?
Override onKeyDown also and return true and not false.
Somnething like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
return false;
}
In the documentation, it is stated that you should return:
true if you handled the event
false if if you want to allow the event to be handled by the next receiver.
Your method is returning false, so you are passing the event to the default key handler
I'm making a game with OpenGL Es 1.1 and i want to implement back button or menu button functionality to my game.(I mean hardware buttons).I have some subClasses so what i have to do when i want to handle hardware button press from subClasses?
You need to implement an onKeyDown listener and check to see what key is pressed.
Sample:
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
handleMenuButton();
return true;
case KeyEvent.KEYCODE_BACK:
handleBackButton();
return true;
}
return super.onKeyDown(keycode, e);
}
Also note that for the back and menu buttons to fire reliably, you need to set setFocusableInTouchMode to true. See the devguide here (scroll down to "Touch Mode").
I have a Droid X, which has a physical camera button. I am using the example used here: http://marakana.com/forums/android/examples/39.html
The app sort-of works. The on-screen button captures and displays the preview image. But if I push the physical camera button, the app crashes.
How should I handle this, and more importantly - is this going to cause problems across different devices that do / do not have physical buttons?
You need to override onKeyDown in your Activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_CAMERA:
// handle the event here
}
}
return super.onKeyDown(keyCode, event);
}
I want to use Menu and OnkeyDown in same activity but it is not working, when I use OnkeyDown then menu disappear from the activity. I want to make a action using virtual keyboard's enter key. When I press enter key of virtual keyboard one toast should be appear.
Please help.
Thanks in advance
You have to return false outside the switch statement of the onKeyDown but true for every key you handle:
public boolean onKeyDown(int KeyCode, KeyEvent event) {
switch (KeyCode) {
case KeyEvent.KEYCODE_CAMERA:
//take photo
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//lower volume
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
//raise volume
return true;
}
return false;
}
This works for 2.3.3