onGenericMotionEvent on android button - android

I did not able to set the onGenericMotionEvent on the button or any view. Means I did not get any response from onGenericMotionEvent method.
Below is my code:
<Button
android:id="#+id/btnMouse"
android:layout_width="match_parent"
android:layout_height="match_parent" />
my Java file coding:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remote_controls);
btn1 = (Button) findViewById(R.id.btnMouse);
}
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
int pointerCount = event.getPointerCount();
switch (event.getAction() & event.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
Log.e("Mouse: ", "Right Click");
return true;
case MotionEvent.ACTION_DOWN:
Log.w("Mouse: ", "Left Click");
return true;
case MotionEvent.ACTION_HOVER_MOVE:
if (pointerCount == 1) {
Log.e("Mouse: ", "Move");
} else if (pointerCount == 2) {
Log.e("Mouse: ", "Scroll");
}
return true;
}
return false;
}
and when i use setOnTouchListener on button it works perfect, but i unable to get the MotionEvent.ACTION_HOVER_MOVE on my button, When i read documents it specify that setOnTouchListener can not handle MotionEvent.ACTION_HOVER_MOVE so that's why i want to use the onGenericMotionEvent on my button to get MotionEvent.ACTION_HOVER_MOVE action.
or any other way to implements MotionEvent.ACTION_HOVER_MOVE on the button?
But i don't know how can do that, please help
EDITED
By Android Button, I want to simulate the real mouse, Means when user dragged over it than i want to move my laptop mouse.
When i use MotionEvent.ACTION_MOVE: it works and it move the my laptop mouse, but before my mouse move it change the location of laptop mouse pointer where it is click on button, so ignoring that Android provide ACTION_HOVER_MOVE it is not perform ACTION_DOWN unlike ACTION_MOVE

Hey as I got your problem you want a hover effect to work on button when you touch on it.
You can use the code below just pass instance of your Button in this method and also customise colors for hover effects:
public void buttonEffect(View button){
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.getBackground().setColorFilter(Color.parseColor("#00BFFF"),PorterDuff.Mode.SRC_ATOP);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP: {
v.getBackground().clearColorFilter();
v.invalidate();
break;
}
case MotionEvent.ACTION_CANCEL:{
v.getBackground().clearColorFilter();
v.invalidate();
break;
}
}
return false;
}
});
}
Hope this help you...!!!

Related

Android onTouch() makes sound like double tap

I've implemented onTouch for my layout to function as buttons.
It all works great besides the fact that a 2 clicks sound is made sometimes as touching the button.
I've tried to debug it but I wasn't able to understand what is the problem.
Here is the onTouch code :
button.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
Log.d(TAG, "onTouch "+event.getAction());
switch (event.getAction())
{
case MotionEvent.ACTION_HOVER_ENTER:
v.setBackgroundResource(R.color.main_menu_buttons_bg_pressed);
break;
case MotionEvent.ACTION_HOVER_EXIT:
v.setBackgroundResource(R.color.background_color);
break;
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.color.main_menu_buttons_bg_pressed);
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "on click");
v.performClick();
v.setBackgroundResource(R.color.background_color);
break;
default:
// empty
}
return false;
}
});
As you can see I've added log messages, which usually prints :
onTouch 0
onTouch 2
onTouch 2
onTouch 2
onTouch 2
onTouch 1
on click
so it should be ok but yet the clicks are heard twice (sometimes, can't determine exactly when it happens)
it's the expected behaviour du of v.performClick(); , when the view has also an OnClikListener: From GrepCode
2480 public boolean More ...performClick() {
2481 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
2482
2483 if (mOnClickListener != null) {
2484 playSoundEffect(SoundEffectConstants.CLICK);
2485 mOnClickListener.onClick(this);
2486 return true;
2487 }
2488
2489 return false;
2490 }

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: 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

Android to detect when you are holding down a button

I need to be able to tell when the user is holding a button down and when the user lets go. This is different from onClickListener and onLongClickListener. How would i go about doing something like this?
For example I press a button that starts a chronometer.(pseudo code)
if ButtonIsBeingPressed
{
chronometer start(); //and keep going
}
else chronometer stop();
//or on release or something
}
Look into the OnTouchListener it has MotionEvents for Down (press) and Up (release):
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Start
break;
case MotionEvent.ACTION_UP:
// End
break;
}
return false;
}
});

Android onTouch with onClick and onLongClick

I've got a custom view which acts like a button. I want to change the background when user press it, revert the background to original when user moves the finger outside or release it and I also want to handle onClick/onLongClick events. The problem is that onTouch requires me to return true for ACTION_DOWN or it won't send me the ACTION_UP event. But if I return true the onClick listener won't work.
I thought I solved it by returning false in onTouch and registering onClick - it somehow worked, but was kinda against the docs. I've just received a message from an user telling me that he's not able to long-click on the button, so I'm wondering what's wrong here.
Part of the current code:
public boolean onTouch(View v, MotionEvent evt)
{
switch (evt.getAction())
{
case MotionEvent.ACTION_DOWN:
{
setSelection(true); // it just change the background
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
{
setSelection(false); // it just change the background
break;
}
}
return false;
}
public void onClick(View v)
{
// some other code here
}
public boolean onLongClick(View view)
{
// just showing a Toast here
return false;
}
// somewhere else in code
setOnTouchListener(this);
setOnClickListener(this);
setOnLongClickListener(this);
How do I make them work together correctly?
Thanks in advance
onClick & onLongClick is actually dispatched from View.onTouchEvent.
if you override View.onTouchEvent or set some specific View.OnTouchListener via setOnTouchListener,
you must care for that.
so your code should be something like:
public boolean onTouch(View v, MotionEvent evt)
{
// to dispatch click / long click event,
// you must pass the event to it's default callback View.onTouchEvent
boolean defaultResult = v.onTouchEvent(evt);
switch (evt.getAction())
{
case MotionEvent.ACTION_DOWN:
{
setSelection(true); // just changing the background
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
{
setSelection(false); // just changing the background
break;
}
default:
return defaultResult;
}
// if you reach here, you have consumed the event
return true;
}

Categories

Resources