How to get android activity or controls touch count? - android

I want to get activity or any control element's touch counts.
Like Android 4.2 on nexus Developer options Enable while touching 7 times.
What is the suitable event to handle it ?

You are getting 2 count's because the touch event are called every time a touch event is snet, like tump down and tump up (for example)
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_DOWN:
//add here the counter if you want when screen pressed
break;
case MotionEvent.ACTION_UP:
//add here the counter if you want when touch released
break;
default:
return super.onTouchEvent(event);
}
}

You can use a View.OnTouchListener, overriding the onTouch method :
#Override
public boolean onTouch(final View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// increment counter
break;
}
}

Related

Android MotionEvent to detect only ACTION_MOVE

As per android MotionEvent documents: A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP). The motion contains the most recent point, as well as any intermediate points since the last down or move event.
ACTION_MOVE Android doc
so when i apply a setOnTouchListene on my view is perfectly works, It give me ACTION_DOWN, ACTION_UP, and ACTION_MOVE
but my problem is i just want to ignore a ACTION_DOWN events exactly before ACTION_MOVE. Because ACTION_MOVE event occur only after the ACTION_DOWN event as per its documents.
my coding:
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.e("Mouse: ", "Click");
break;
case MotionEvent.ACTION_MOVE:
Log.e("Mouse: ", "Move");
break;
}
return false;
}
});
So, there is any why to ignore ACTION_DOWN event. Because user only want to move not want to click, and ACTION_MOVE is also occur ACTION_DOWN before it execute it self.
Thanks...
According to your comment - you can play with counter. For example:
private static int counter = 0;
...
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.e("Mouse: ", "Click");
break;
case MotionEvent.ACTION_MOVE:
counter++; //check how long the button is pressed
if(counter>10){
Log.e("Mouse: ", "Move");
}
break;
case MotionEvent.ACTION_UP:
if(counter<10){
//this is just onClick, handle it(10 is example, try different numbers)
}else{
//it's a move
}
counter = 0;
break;
}
return false;
}
});

Android Button KeyDown - KeyUp | Different Button Events

I have a easy question about devoloping android app.
Question is diffrent type of button.
For example, when my finger on the button, music is play but when my finger up to button music is stopped. How can i do this. İt's probably diffrent type of button event's but I don't know.
I google ıt but I find anything.
PS: I can starting, stopping process on normal button. But i can't do like this.
PS2: When I search on forums somebody recommend to using MotionEvent but I guess my motionEvent codes are broken.
play1.setOnClickListener(new View.OnClickListener()
{
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
m1=MediaPlayer.create(MainActivity.this, R.raw.be);
m1.start();
m1.setLooping(true);
break;
case MotionEvent.ACTION_UP:
m1.stop();
m1.release();
break;
default:
break;
}
return false;
Okay okay ı got it. This is my answer.
play1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
m1=MediaPlayer.create(MainActivity.this, R.raw.be);
m1.start();
m1.setLooping(true);
return true;
case MotionEvent.ACTION_UP:
m1.stop();
m1.release();
break;
}
// tell the system that we handled the event but a further processing is required
return false;
}
});

How to perform an action as long as a Button is kept pressed

Hi and Thanks for your help.
I need a button to perform the following behavior:
when the button is pressed an action begins (in this case a view scrolls)
as long as the button is kept pressed the action continues (the view continues scrolling)
when the button is released the action (the scrolling) stops.
I have tried this, but does not work:
Button left = (Button) findViewById(R.id.left);
left.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("","left");
return false;
}
});
I think you are looking for a combination of OnTouchListener and MotionEvent.ACTION_DOWN
Edit:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// TODO start scroll
break;
case MotionEvent.ACTION_UP:
// TODO stop scroll
break;
default:
break;
}
return false;
Hope this helps.. :)
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
#Override public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Start your work
break;
case MotionEvent.ACTION_UP:
// stop the work
break;
case MotionEvent.ACTION_CANCEL:
// stop the work
break;
}
return false;
}
});
Use MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP for detecting action up as well as down
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//start action here
break;
}
return super.onTouchEvent(event);
}
By default you can use onlongclick listener, But in your case it won't give effect as its max time 1-2 sec, so apply motion event on button and apply your logic..
You can use MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP for detecting action up and down
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//start action
break;
case MotionEvent.ACTION_UP:
//stop action
break;
}
return super.onTouchEvent(event);
}
make new scroll view
ScrollView sv=new ScrollView(this);
and when you click/start button. then start thread which increment int value and scroll, and on key action down. stop the thread.
//start thread which incrment value one by one
sv.scrollBy(x, y); //x the amount of pixels to scroll by horizontally
//y the amount of pixels to scroll by vertically

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.
Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.
Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.
boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
pressed = true;
break;
case MotionEvent.ACTION_MOVE:
//User is moving around on the screen
break;
case MotionEvent.ACTION_UP:
pressed = false;
break;
}
return pressed;
}

Android: Long touch on Button while MotionEvent.ACTION_MOVE

Ok... in my app i update the layout on MotionEvent.ACTION_DOWN and then i check the motion event coordinates to locate my buttons. I can show a toast when finger is released on different buttons. The problem is i need a long touch on my buttons to call another action without conflicting with the MotionEvent.ACTION_UP. Implemented a long click handler but since i don't 'click' its not working. Hope you guys understand my problem.
Whats the best way to get my app working as intended?
My class implements OnTouchListener, OnGestureListener
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// UPDATE LAYOUT
break;
case MotionEvent.ACTION_UP:
// GET BUTTON X Y
if (x and y match the button location){
// DO ACTION
}else{
// DO NOTHING
}
// CHANGE LAYOUT TO INITIAL STATE
break;
case MotionEvent.ACTION_MOVE:
break;
}
return false;
mybutton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// DO STUFF
return true;
}
});
}
just try to return false in your onTouch(...) method and use onLongClickListener(...) as usual

Categories

Resources