I know that there are listeners for finger touch etc.. but I wonder how I can decet a finger drag up/down/left/right.
Is there any kind of listener for that?or it's some math involved.
Use this
#Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
Try taking a look at the Android Developer Resources sometime.
Related
I have a block, I drag it somewhere then this cell transform to yellow then I exitted this cell, It transforms again gray but, When I drop it somewhere Everywhere is magenta, How can I paint only that block When I drop it, You can check pictures to understand my problem
Best regards
public boolean onDrag(View v,DragEvent event)
{
int action=event.getAction();
switch (action)
{
case DragEvent.ACTION_DRAG_STARTED:
Log.i("Script",num+"-ACTION_DRAG_STARTED");
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
return (true);
}
return (false);
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Script",num+"-ACTION_DRAG_ENTERED");
v.setBackgroundColor(Color.YELLOW);
break;
case DragEvent.ACTION_DRAG_LOCATION:
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.GRAY);
break;
case DragEvent.ACTION_DROP:
View view=(View) event.getLocalState();
ViewGroup owner=(ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container=(LinearLayout) v;
container.addView(view);
view.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.MAGENTA);
break;
}
return true;
}
In DragEvent.ACTION_DRAG_ENDED event you should find view.getX() and view.getY(). This will return x and y co-ordinate in pixels then according to your requirement you should calculate nearest view which occupy this blocks and change its color to MAGENTA.
I have this code for my MotionEvents in Android:
#Override
public boolean onTouchEvent(MotionEvent event){
int action = event.getActionMasked();
switch (action){
case MotionEvent.ACTION_UP:
mGameview.moveUp(30);
break;
case MotionEvent.ACTION_DOWN:
mGameview.moveDown(30);
break;
}
return true;
}
However, instead of these things only triggering when I drag my finger up/down, they both happen simultaneously no matter what I do in the app, for example when I tap, drag left and right.
Try to get action by:
int action = event.getAction() & MotionEvent.ACTION_MASK;
I have the following code, all I want is to detect touch on a edit text and scroll the scroll view:
View.OnTouchListener buttonFocus = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("TOuch");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("TOuch down");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case MotionEvent.ACTION_UP:
System.out.println("TOuch up");
((ScrollView) findViewById(R.id.m_scroll)).fullScroll(View.FOCUS_DOWN);
v.performClick();
break;
default:
break;
}
return false;
}
};
While as the keypad pops up at the start of activity the scroll does not occur, it only scroll when I touch the view for the second time. Am I doing something wrong here? Will implementing a thread help?
Returning true in that last line of yours might solve your problem.
Returning false means that the onTouch has been used completely and won't entertain any further touch events
In my application I want to add a simple animation to buttons and other views acting as buttons.
To do this I set a custom onTouchListener to all views and call startAnimation on them.
My onTouch method looks like this:
public boolean onTouch(View v, MotionEvent event) {
// Only show animation when enabled.
if (v.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.startAnimation(shrink);
break;
case MotionEvent.ACTION_UP:
v.startAnimation(grow);
break;
}
}
return v.onTouchEvent(event);
}
This works ok as the views are resized to a smaller size while the user presses the button and returns to the original size when the user releases the finger.
However for some reason other buttons that lie near the touched button also get the UP event so they get a small animation flicker as well.
Why is this, and more importantly, how do I prevent this annoying behaviour?
Edit: Just to be clear. The neighbour views are also registered to the same listener instance, but they are not touched by my finger.
The view to wich you have registered listener then should only get notified then also use following approach.
You can explicitly check what is id of View and then only start animation.
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.desired_view_id) {
// Only show animation when enabled.
if (v.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.startAnimation(shrink);
break;
case MotionEvent.ACTION_UP:
v.startAnimation(grow);
break;
}
}
}
return v.onTouchEvent(event);
}
The problem was using the same Animation instance on several views. Creating seperate instances for each view fixed the problem.
Alright so i have 4 image views..
How can i control what happens when they are pressed down and up
So say like:
one of the images is press down a textview gets changed to 1 but when they let go of that one the text will change back to 0?
You need to use an OnTouchListener and have it listen for TouchEvents on your Views.
For Example:
MyTouchListener l = new MyTouchListener();
view.setOnTouchListener(l);
Here is an example of what MyTouchListener could look like:
class MyOnTouchListener implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
}