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) {
}
}
Related
I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
I want to increase the forward/rewind speed if the button is pressed continuously.
I created my customized media controller from here.
What would be the good way to speed up the forward/rewind process?
I got some idea form here. Finally I implemented it too but the touch event worked successfully in my emulator or and android mobile too but didn't worked for android stb. Other solutions rather than using it would be appreciated.
How about you monitor the MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP and see how long the button is pressed.
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// the button is down monitor time to see how long it is down or anything you want
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// the button is up reset your timer
}
}
};
Hi I want to find out if a tablet is hit with a stick lightly. I thought maybe implementing accellerometer should do it. But all I see people are trying to find shake movements with it. Is there any way I can find if a tablet is tapped? Thanks.
EDIT: By saying tapping I mean tapping on top of the tablet. Not in screen.
I'd say the easier route for detecting a tap would be by measuring the time between a MotionEvent.ACTION_DOWN and the corresponding ACTION_UP - if it's less than, say, 350 milliseconds, the user was probably doing a tap and not a press or some other sort of gesture.
Consider something like the following, where TAP_CONSTANT would be an integer constant for the tap duration:
private class TapListener implements OnTouchListener
{
long time;
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
time = System.currentTimeMillis();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
if ((System.currentTimeMillis() - time) <= TAP_CONSTANT)
{
// We have a tap event, call our tap logic methods!
return true;
}
}
return false;
}
}
I have an App with 10 Buttons. Everytime the User presses on one Button A TextView should change. And if the User changes the focus and ,oves its finger to right, to the next button(without taking the finger off the screen) the seond button should be focused.
I tried it with setting an OnTouchListner to all buttons, but once the finger is moved the focus still stays on the first button:
btn1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
txt1.settext("1");
return false;
}
});
I hope you understood what I mean and can help me with this.
Thanks
Edit:
I found an Application which does that, here is a Video, so you can visualise what I mean.
Notice how I move the mouse(the finger in this case) and the Boxes change its focus:
http://www.youtube.com/watch?v=MRVFpNrBmsA&feature=youtu.be
To start in your onTouch() you should handle the events...
Something like
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
txt1.settext("1");
} else if(event.getAction() == MotionEvent.ACTION_HOVER_EXIT){
txt1.settext("0");
}
return true;
}
Not sure if those are the proper actions to check against but it is just to give the idea.
Also I believe you should return true otherwise it means that the event wasn't processed and it gets stuck.
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);
}