I am trying to port my Android app to work well on tablets with Honeycomb. One issue I am having is when the keyboard is showing and the back button turns into a down arrow, I can't seem to pick it up as not only a back button action, but as a keyevent in general. Here is my code:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "keycode: " + event.getKeyCode());
}
So when the arrow is pointing down, I don't get a LogCat read out, and it appears it doesn't even know the down arrow is a KeyEvent. Has anyone else run into this? How can I fix it?
Look at the View.onKeyPreIme() method.
Although reading your question and your reply to Mark, I'm unsure as to whether you're writing an app or an input method.
Related
I am building a custom camera app and have got the basics to work. I have also been able to block the camera button from initializing the real camera app. The only thing that I would like to do is build in autofocus when the camera button is half pressed.
I am comfortable using camera.autofocus, but cannot find a way to listen for the camera button to be halfway pressed (like the default camera app does) to start the autofocus call.
Is there a keycode or another way to listen for the camera button being depressed to its half way point?
I got a little creative and just toasted any key down event in android. I ended up finding out that the key code for camera focus is 80 this way. This also matches up with the android documentation once I knew what I was looking for.
http://developer.android.com/reference/android/view/KeyEvent.html
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_DOWN){
Toast.makeText(this, new Integer(keyCode).toString(), Toast.LENGTH_LONG).show();
return true;
}
return false;
}
Hope this helps others.
The camera button is a virtual button on the screen, I am not sure how it can the half pressed, or even could be mimicked in any way.
I am developing an application that I don't want user to touch home/back button. Trust me, I have a good reason for it.
What I need to do is to disable home/back or even the keyboard using terminal.
I've look through commands in adb shell already and I cannot find any command to fix this.
I'm not sure about AVD, but in real phone this buttons are hard buttons. So, no software can remove them. All you can do is to override the behaviour for this buttons using onKeyDown() method.
Using the following piece of a code in an Activity subclass would work for intercepting key events:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return true;
// return super.onKeyDown(keyCode, event);
}
The back key is the constant KeyEvent.KEYCODE_BACK; the home button should be similar.
I'm a beginner at programming in general. I don't even know how to begin to search for something like this. What i want to do is make a simple text app that counts. Mainly i want to count how many fish i catch. I want to be able to use the volume rocker when the screen is locked to count how many fish i catch. I haven't started on the app yet, so i don't have any coding to post. If someone can point me in the right direction, that would be wonderful. Thanks in advanced.
This code will get started listening for the volume button presses. I imagine if you want to do it while the screen is off you'll have to acquire some kind of wakelock that lets the screen shut off but still keeps your app running.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
//Do something
}
return true;
}
I know this topic was asked trillions of times throughout the web, but there is no one place answering it.
in my application, I have a welcome screen. when I run the program end-to-end everything is just fine - open/close activities and show dialogs of all sorts.
but, when I reach the welcome screen and from there pressing the BACK button - everything becomes messy:
1) the dialog i want to show cause BadTokenException (i'm using this and not getApplicationContext() ).
2) I tried use try/catch to catch the exception - and it really passed the showDialog line. but then, in the 2nd. run, when reaching a showDialog expression, it throws IllegalStateException: View.com.android.internal.policy.impl.PhoneWindow$DecorView#44a59830 has already been added to the window manager.
moreover, I understand that pressing HOME causes the onPause in the activity, while BACK causes onDestroy, but what goes wrong ??
i'm trying to fix that issue for more than a week, but with no success.
any ideas ?
I didn't get you complete, But i think you need to do some stuff when you back button is pressed. So you can override you back button in you welcome screen activity or any other activity and add you code there
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// add you code here
return true;
}
return super.onKeyDown(keyCode, event);
}
I'm writing an Android game that runs in fullscreen landscape mode, and has buttons placed at the bottom left and bottom right of the window. The problem is that one of these buttons is (on many phones) right next to the Menu button, so the player might accidentally press Menu instead.
If it is pressed briefly, I simply pause the game and show the in-game menu. No problem there.
But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen. Since it gets in the way, and is completely useless in this Activity, I would like to disable it.
I tried the following approaches.
Via InputMethodManager
From: Hide soft keyboard on activity without any keyboard operations
Since I have only one view (a GLSurfaceView) I tried this in my Activity.onCreate():
InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);
It doesn't work: the soft keyboard still comes up on Menu long-press.
Via the AndroidManifest.xml
From: How to stop the android soft keyboard from ever coming up in my entire application
I added this to my manifest:
<activity
android:windowSoftInputMode="stateAlwaysHidden"
>
Does a great deal of nothing as well.
So... is there even a way? How?
Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// From the docs:
// "Note that in order to receive this callback, someone in the event [chain]
// must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Override default handling, and don't pop up the soft keyboard.
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
openOptionsMenu();
return true;
}
return super.onKeyUp(keyCode, event);
}
But it feels like a hack, so I'm hoping that someone comes up with a better solution.
But if the button is held down longer,
Android opens up the soft keyboard on
the bottom half of the screen.
What phone do you have? Are you sure? I've never once seen that happen and I just tried it and it doesn't work on my phone.
Also, that sounds like a user problem. Don't try to subvert the user. If the user REALLY wants to open a keyboard in your app, you should let them and if it's useless, they'll hit back and it will go away.
A more concerning issue should be that your buttons are so close to the menu buttons.
Try using hideSoftInputFromWindow() instead. According to the documentation:
request to hide the soft input window from the context of the window that is currently accepting input.
use android:windowSoftInputMode="adjustPan" in android manifest.
I think this is best choice to prevent the view goes up.