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);
}
Related
I've got a Samsung tablet SM-T560 on which when pressing the touch 'menu' key (next to the hardware 'home' button) opens the system menu for 'Active apps' where you can close or navigate to a previously opened app.
How can i override this functionality?I want to disable showing active apps upon this menu button click.
you need to override the menu button using the onKeyDown(int, KeyEvent) method in your activity. The following code snippet should be a good start for you:
#Override
public boolean onKeyDown(int keycode, KeyEvent event) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
//Your functionality here
return true;
}
return super.onKeyDown(keycode, event);
}
I'm looking is it possible to capture MotionEven data (pressure) on keyboard (in my app)? Or is there way to intercept all touches on screen in my app (something like full screen mode).
Try it:
#Override
public boolean onTouchEvent(MotionEvent me) {
//for example
if (me.getAction() == MotionEvent.ACTION_UP) {
}
}
I want to press hardware button Next will show Next Screen and press hardware button Pre will show Pre Screen (ViewFliper)
What do you know event press hardware button Next and Pre in Android ?
How catch event press hardware button Next and Pre in Android ?
You can watch image at here: enter link description here
Thanks!
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mViewFliper.showPrevious();
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
mViewFliper.showNext();
return true;
}
return super.onKeyDown(keyCode, event);
}
Although I can not express enough how utterly horrible it is for an app to do something like that.
Id like my full screen mobclix ads to be non destroyable for 2 seconds. Right now users can press the back button on the android phone before the ad even shows up. How can I accomplish this?
All you need to do is intercept the back button press which you can do by overloading:
onBackPressed()
On older devices this won't quite work and you'll need to do something like:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Right now when you hold down the menu button on my android device it pulls up a soft keyboard.Is there a way to override this? I would rather choose what happens on a long touch of this button.
When using onKeyLongPress it only detects when the "Back" button is held down. How can I make this work for the menu button?
For this, you can use the onKeyLongPress()-method, offered by the KeyEvent.Callback-class (can be used in Activity's too, since they are a subclass of the KeyEvent.Callback-class).
There is also a little trick to make this work: You'll have to tell Android to track a long-press click on the "Menu"-button as the onKeyLongPress()-method will not be triggered otherwise. This is done in the normal onKeyDown()-method.
So your code might look like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
// Do what you want...
Toast.makeText(this, "I'm down!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyLongPress(keyCode,event);
}
A great article with further informations can be found on the Android Developer Blog.