How to understand when user touch the screen in android? - android

Is there any way to know when user touch the screen?
Actually i want to know user is using the phone or not. And i thought if user doesn't touch the screen he/she doesn't use the phone. I've already goggled it but i couldn't find any proper things, that's why i want to ask this question in here, and i couldn't find any similar question in here.
I find some code like this :
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
but i don't know how to use properly.
thanks in advance.

You can check the last touch of screen using
System.curTimeMillis();
And use
long lastTouchTime;
#Override
public boolean onTouchEvent(MotionEvent event) {
lastTouchTime = System.curTimeMillis();
return true;
}
Or you can check is screen touched now or not :
boolean isScreenTouchedNow;
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
isScreenTouched = true;
break;
case MotionEvent.ACTION_UP:
isScreenTouched = false;
break;
}
return true;
}
public boolean isScreenTouchedNow()
{
return isScreenTouched;
}

Related

Android How Can I move an ImageView when the user clicks a button (For a game)

I have yet to find the solution to this problem. I'm trying to make a game. I want to make an image move to the left, right, up or down when a button is clicked. Please help me as I have struggled with this problem for two days. Please show and explain to me how to move an ImageVıew as explained above.
Thanks in advance.
bt = (Button) findViewById(R.id.button2);
bt.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
playerY+=20;`
player.setY(playerY);
return true;
}
});
Try this
UpButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Do something
return true;
case MotionEvent.ACTION_UP:
// No longer down
return true;
}
return false;
}
});
Action Down means the button is clicked and action up gets triggered when the finger is no longer on the button

Android: How can I call a method after a view touched

I want to call a method after my button get touched and call another method after touch finished,
is it possible?
Try This Code it May help you
yourButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Do what you want
return true;
}
return false;
if(event.getAction() == MotionEvent.ACTION_UP){
// Do what you want
return true;
}
return false;
}
});
It seems like you have some research to do. What kind of view do you have? You need something like this:
#Override
public void onClick(View v) {
//Call your method here
}
But it is impossible to know what exactly you need, without knowing anything about what you are doing.
EDIT: You changed your question. This is what you're looking for.

remove default selection without disabling web view's hyperlinks and buttons

I am using btwebview to get text selection and handle it. The problem is when I use longPress with gestureDetector the default selection is also being launched, if I override onTouchEvent and return true, the problem is solved but I cannot click on any button or highlighted link on the webview, so I cannot access footnotes or videos inserted in the webview and shouldOverrideUrlLoading stops getting called.
public void init(Context context) {
System.out.println("BTWebview init");
this.context = context;
this.getSettings().setJavaScriptEnabled(true);
gestureScanner = new GestureDetector(this);
this.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
System.out.println("event "+event.toString() );
return gestureScanner.onTouchEvent(event);
}
});
setInitialScale(100);
addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
}
#Override
public boolean onTouchEvent(MotionEvent event) {
System.out.print("on touch event "+ event.toString());
return true;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
System.out.println("gesture listener onLongPress");
mSelection = true;
if(!this.isInSelectionMode()){
System.out.println("onLongClick in selection mode");
mSelection = true;
}
this.loadUrl("javascript:android.selection.longTouch();");
mScrolling = true;
System.out.println("onLongClick");
}
The reason that the webview has stopped responding to touches is because that functionality is implemented in the superclass' onTouchEvent.
So to get it to work again you will need to call super.onTouchEvent(event) somewhere in your onTouchEvent. Obviously just always calling it would get you back to where you started.
To achieve what you want to do you will need to call super.onTouchEvent only when you have not already detected that the event is a long press event. The simplest way to do this would be to store the pointer ID from the MotionEvent that is passed in onLongPress (you should be able to assume it will be the pointer at index 0 because a long press is by definition a single touch event).
Once you have this your onTouchEvent could look something like this
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerId(0) != self.lastLongPressPointerId) {
return super.onTouchEvent(event);
}
return true;
}
You might also need to watch for the ACTION_UP and ACTION_CANCEL events relating to the pointer and stop looking for it after that just incase the system decides to reuse the pointer ID.

getPressure and getSize always return the same value

Does anyone have any idea why getPressure() returns always 0.4 and getSize() returns 0.12...?
Code snippet:
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
System.out.println(event.getPressure());
System.out.println(event.getSize())
return true;
}
I think getPressure might not work, because there is no any physical functionality. But I can't figure out why getSize doesn't work?
TF300TG is android4.0, in Settings->Developer Options, check the "Pointer Location".
Could it be that these are the values generated at the very first part of the motion event only?
Try outputting the values on touch MOVE, as this is where the shape normally changes. This should give you some variation.
#Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
System.out.println(event.getPressure());
System.out.println(event.getSize());
break;
}
return true;
}

Stylus only input-mode on Android

How can I turn on the "pen-only" mode programatically on the Lenovo Thinkpad Tablet running ICS 4.0.3?
I'm very sorry that I can't answer your question, though I think WebnetMobile might be right - there doesn't seem to be an API method for pen-only mode. However if you go to this article http://knowledge.lapasa.net/?p=490 it states that you should focus on the diameter of the input device (the touch diameter of a finger is much greater than that of a pen stylus).
As for WebnetMobile saying it is pointless to use a tablet in pen-only mode, I'd say that it is a longed for feature of any artist or note taker to be able to rest your hand on the surface upon which you're drawing/writing.
If you are developing an app, you can actually implement this. If your stylus can "hover" on your screen before it touches, you can add an OnHoverListener on the highest View in your activity to set a global variable isPen to true. Then override dispatchTouchEvent() in your activity and check the variable before passing on the event in your app.
rootView.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View view,MotionEvent event) {
isPen = true;
return true;
}
});
And
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if(isPen) {
return super.dispatchTouchEvent(event);
} else {
return true;
}
}
I know this is old, but I found nothing else on this problem and I eventually come out with this solution: you need to extend your view and override onTouchEvent:
#Override
public boolean onTouchEvent(MotionEvent event) {
// Disable multitouch
if (event.getPointerCount() != 1)
return false;
// If not stylus return
MotionEvent.PointerProperties pp = new MotionEvent.PointerProperties();
event.getPointerProperties(0, pp);
if (pp.toolType != MotionEvent.TOOL_TYPE_STYLUS)
return false;
// Dispatch event to the original view
return super.onTouchEvent(event);
}

Categories

Resources